Three major text processing tools in Linux

1.grep

meaning:
  grep (Globally search a Regular Expression and Print) is a powerful text search tool that can search for text using specific pattern matching (including regular expressions) and output matching lines by default.
  The Unix grep family includes grep, egrep and fgrep. The commands of egrep and fgrep are only slightly different from grep. egrep is an extension of grep, supporting more re meta characters, fgrep is fixed grep or fast grep, they treat all letters as words, that is, meta characters in regular expressions represent their own literal meaning , No longer special. Linux uses the GNU version of grep. It is more powerful. You can use the functions of egrep and fgrep through the -G, -E, and -F command line options.
 The way grep works is this, it searches for string templates in one or more files. If the template includes spaces, it must be quoted, and all strings after the template are treated as file names. The search results are sent to standard output without affecting the content of the original file. grep can be used in shell scripts because grep returns a status value to indicate the status of the search. If the template search is successful, it returns 0, if the search is unsuccessful, it returns 1, if the search file does not exist, it returns 2. We can use these return values ​​to perform some automated text processing.

grep format
grep match condition processing file

Example:
grep root passwd ## Filter root keyword
Insert picture description here
grep -i root passwd ## Ignore case
Insert picture description here
grep ^ root passwd ## Start with root
Insert picture description here
grep root $ passwd ## End with root
Insert picture description here
grep -E “\ <root” passwd ## The root character must not have the character
Insert picture description here
grep -E "root \>" passwd ## The root must not have the character
Insert picture description here
grep -number ## Display the filter line and the upper and lower lines
Insert picture description here
grep -n ## Display the matching line No.
Insert picture description here
grep -A ## Display the filter line and the lines below the filter line
Insert picture description here
grep -B #Display the filter line and the lines above the filter line
Insert picture description here
grep -v ## Reverse filter, display the lines except the filter line
Insert picture description here
grep character number matching rules

Example:

w....s ##以w开头s结尾中间4个任意字符
Insert picture description here

.....s ##以s结尾,前面5个任意字符
Insert picture description here

* ##字符出现0次到任意次
Insert picture description here

? ##0到1次
Insert picture description here

Insert picture description here

{n} ## appears n times
Insert picture description here

{m, n} ## appears m to n times
Insert picture description here

{0, n} ## Occurrence 0-n times
Insert picture description here

{, n} ## Occurrence 0-n times
Insert picture description here

{m,} ## appears at least m times
Insert picture description here

(lee) {2} ## lee string appears twice
Insert picture description here

Practice script
Please display the user name in the system that can be switched by the su command
Insert picture description here
Insert picture description here

2.sed (stream editor)

(1) Concept of sed command
sed is a linux command, the function is similar to awk, the difference is that sed is simple, the function of column processing is worse, the function of awk is complex, and the function of column processing is stronger.
sed is used to manipulate text in pure ASCII code.
During sed processing, the current line is stored in a temporary buffer, called "pattern space", and you can specify which lines are processed only.
sed processing that meets the pattern conditions, not processing that does not meet the conditions, after the processing is completed, the content of the buffer is sent to the screen, and then the next line is processed, so that it repeats continuously until the end of the file.
The original content was placed on the disk. Now when the sed command is used for processing, the content is dropped into a place in the memory for user processing. This processing space is also called the pattern space.
(2) Command format
sed parameter command processing object
sed parameter processing object -f processing rule file

(3) Handling of characters

p display
d delete
a Add to
c replace
w Write the matching line to the specified file
i insert
r Integrated document

1) p

sed -n 5p westos ##显示westos中的第五行
Insert picture description here

sed -n 3,5p westos ##显示westos文件中的3到5行
Insert picture description here

sed -ne "3p;5p" westos ##显示3行和5行
Insert picture description here

sed -ne '5, $ p' westos ## Display 5 to the last line in the westos file
Insert picture description here

sed -n '/^#/p' fstab ##显示fstab文件中以#开头的行
Insert picture description here

2) d ## Delete

sed 5d westos ##删除westos文件中的第五行
Insert picture description here

sed '/^#/d' fstab ## fstab文件中以#开头的行
Insert picture description here

sed '/^UUID/!d/' fstab ##除了以UUID开头的行不删,其他行都删
Insert picture description here

sed -e '5,$d' westos ##删除第五行到最后一行
Insert picture description here

3) a ## Add

sed -e '$a hello world' fstab ##在fstab文件最后添加一行hello world
Insert picture description here

sed -e '$a hello\nworld' fstab ##在fstab文件最后添加hello world 俩行
Insert picture description here

sed -e '/^#/a hello world' fstab ##在fstab文件中每一行以#开头的行下面添加一串 hello world
Insert picture description here

4) c ## replace

sed -e '/^#/c hello world' fstab ##讲fstab文件中以#开头的行替换成hello world
Insert picture description here

sed '5chello world' westos ##讲westos文件中的第5行替换成hello world
Insert picture description here

5) w ## Write the matching line to the specified file

sed '/^UUID/w westosfile' westos ##把westos中UUID开头的行写入westosfile中

6) i ## insert

sed '5ihello westos' westos ##在westos 文件中的第五行上面插入 hello westos行
Insert picture description here

7) r ## integrated file

sed '5r haha' westos ##将haha文件整合到westos文件中第五行的下面
Insert picture description here

(4) Character replacement

1)sed 's/:/###/g' westos ##将westos文件中所有的:替换成###,其中g代表所有列,不写g将默认第一列
Insert picture description here
Insert picture description here

  1. sed '1,5s/:/###/g' westos ##将westos文件中1-5行的:替换成###
    Insert picture description here

3)sed '1s/:/###/g' westos ##将westos文件中第一行的:替换成###
Insert picture description here

4)sed '1s/:/###/g;5s/:/###/g' westos ##将westos文件中的第一行和第五行中的:替换成###
Insert picture description here

sed 's/\//###/g' westos ##将westos文件中的/转换为###
Insert picture description here

sed 's@/@###@g' westos ##@符号用作分隔符,和/一样,将westos中的/符号转换为###符号
Insert picture description here

sed '/lp/,/shutdown/s/:/###/g' westos ##将westos文件中的lp到shutdowns行的:替换成###
Insert picture description here

sed 's@/@###@g' -i westos ##把sed处理的内容保存到westos文件中
Insert picture description here

3.awk

Instructions: Awk -F separator BEGIN {} {} END {} FILENAME

awk 'BEGIN{print "LINE";N=0}{N++}END{print N} westos ##BEGIN是预处理,awk是逐行处理,N++依次+1,可以统计westos文件的行数
Insert picture description here

NR ## 行 数
NF ## Column 数
FILENAME ## File name
westos ## westos variable value
"westos" ## westos string

awk '{print NR}' westos ##打印westos文件的行号
Insert picture description here

awk '{print NF}' westos ##每行打印westos文件的列数,默认-F分隔为空格
Insert picture description here

awk -F : '{print NF}' westos ##以:为分隔符,逐行打印westos中的列数
Insert picture description here

awk -F : '$7~/bash$/{print FILENAME}' /etc/passwd ##将/etc/passwd文件中是以bash结尾的行的文件名称打印出来
Insert picture description here

Add conditions:
/ Condition 1 | Condition 2 ## Condition 1 or Condition 2
/ Condition 1 || / Condition 2 ## Condition 1 or Condition 2
/ Condition 1 &&& / Condition 2 ## Condition 1 and Condition 2

awk -F : '/bash$/{print}' westos ##将westos中以bash结尾的行打印出来
Insert picture description here

awk -F : '/bash$/{print NF}' westos ##打印westos文件中以bash结尾的行的列数
Insert picture description here

awk -F : '/bash$/||/^adm/{print}' westos ##将westos文件中的以bash结尾的或者adm开头的行打印出来
Insert picture description here

awk -F '/bash$/&&/^root/{print}' westos ##将westos文件中以bash结尾并且root 开头的行打印出来
Insert picture description here

$0 All columns
$1 first row
$2 the second list

awk -F '/bash$/&&/^root/{print $1,$7}' westos ##显示打印westos文件中以bash结尾,root 开头行的第一列和第七列
Insert picture description here

awk -F : '/bash$/&&/^root/{print $0}' westos ##将westos中以bash结尾,root 开头行的所有列打印出来
Insert picture description here

awk -F '$6!~/home/&&/bash$/{print}' /etc/passwd ##/etc/passwd 文件的第六列没有home关键字并且以bash结尾的行
Insert picture description here

Published 35 original articles · praised 0 · visits 1409

Guess you like

Origin blog.csdn.net/qq_44749796/article/details/105148197