Linux text processing tools----------grep&sed&awk

1.grep

grep supports basic regular expressions
egrep supports both basic and extended regular expressions
egrep = grep -E The
grep command is a Linux text processing tool that belongs to the same family as the egrep command, both of which are used to perform repetitions on files and text Tool for searching tasks. We can search files and their contents for useful information by specifying specific search criteria through grep command. grep is a global search for regular expressions and prints out the matching lines. Its fetching data is in a greedy mode, that is, the filtering content will not be missed, but the accuracy will be relatively reduced.

1.1 grep format

grep match conditions to process files

grep match condition processing file name
grep root passwd ##Filter the line where the root keyword is located
grep -i root passwd ##Ignore case and filter the line where the root keyword is located
grep -E "<root" passwd ##Filter the root character without before Lines of characters
grep -E "root>" passwd ##Filter lines without characters after the root character
grep -number ##Display filtered lines and the lines above and below
grep -n ##Display the line number where the matching line is located
grep -A number##displays the filtered line and the following lines
grep -B number##displays the filtered line and the upper lines
grep -v ##reverse filtering

experiment:

cp /etc/passwd .
 ls
 vim passwd
grep root passwd       //搜索含有root关键字的行

grep -E "root|nologin" passwd
grep -e root -e nologin passwd //搜索含有root和nologin的行

grep -i root passwd     //忽略大小写

grep -E "\<root" passwd  //被搜索字符前不能有字符


grep -E "root\>" passwd  //被搜索字符后不能有字符

grep -n  halt passwd     //显示搜索字符所在行的行号

grep -5  halt passwd //显示含有关键字的行和含有关键字行的上下各5行  

grep -B5  halt passwd  //显示含有关键字的行及上5行

grep -A5  halt passwd   //显示含有关键字的行及下5行

grep  root passwd -v   //显示不含有root的行

insert image description here
insert image description hereinsert image description hereinsert image description here
insert image description here

1.2grep number of characters matching rules

regular expression meaning
^ westos starts with the westos keyword
westos $ ends with westos
w…s w starts with s and ends with 3 arbitrary characters
…s 3 arbitrary characters before the end of s
* Characters appear any number of times (ie 0 to any number of times)
? Character occurs 0 to 1 times
+ Character appears 1 to any number of times
{n} character appears n times
{m,n} Character appears m to n times
{,n} character occurs 0 to n times
{m,} Character appears at least m times
(lee){2} lee string appears 2 times
grep root passwd
grep ^root passwd   //以root开头
grep root$ passwd   //以root结尾

insert image description here
lab environment:

vim westos

cat westos
ws
was
wbs
waas
waaas
waaaas
wadads
wadadads

insert image description here

Experiment:
" . " represents any character

grep ws westos
grep w.s westos
grep w..s westos
grep w...s westos

insert image description here
insert image description hereinsert image description here
insert image description hereinsert image description here

2.sed

The full name of sed is stream editor, which is completely different from vim's interactive editing method. As a non-interactive editor, sed uses pre-set editing instructions to edit the input text, and output editing after completion. result. Its function is very powerful, with the support of regular expressions, it can edit a large number of complex texts.

Command format:
sed parameter command processing object
sed parameter processing object -f processing rule file

sed 's/nologin/westos/g' passwd
vim rule
cat rule
sed -f rule  passwd

insert image description here

1.1 Handling of characters

p : show

sed 5p passwd                      //打印第五行
sed -n  5p passwd                 //只输出被处理内容
cat -b passwd | sed -n  3,5p      //3-5行
cat -b passwd | sed -n  '3p;5p'   //3和5行
sed -n  '/^root/p'  passwd        //root开头
sed -n  '/bash$/p' passwd         // bash结尾
sed -n  '5,$p' passwd             //  5到最后以行   
sed -n  '$p' passwd               //最后一行

insert image description hereinsert image description here
insert image description here
insert image description hered: delete

cat -b passwd | sed '5d'   删除第5行
cat -b passwd | sed '5,8d'      5-8行
cat -b passwd | sed -e '5d;8d'  5和8行
sed -e '/^root/d'  passwd          root开头 
sed -e '/bash$/d'  passwd         bash结尾
sed -e 'd'  passwd              删除全部

insert image description hereinsert image description here

insert image description here
insert image description hereinsert image description here
a : add



sed  '2aleo' westos   在第2行后添加

sed  '/hello/aleo' westos     在hello的下面添加

sed  '$aleo' westos    添加到最后一行

sed  '$aleo\nwestoslinux' westos  最后添加两行 leo、westoslinux

insert image description here
insert image description here
insert image description here
c : replace

sed  '1chhaha' westos  将第1行换成hhaha

sed  '/linux/chaha' westos  将linux换成haha

insert image description herew Write the matching line to the specified file
sed '/^UUID/w westofile' westos ##Write the line starting with UUID in westos into westosfile
insert image description here
i: insert
sed '/westos/iwestosfile' westos
insert image description here
r: integrate the file

sed '2rlee' westos
sed '/hello/rlee' westos

insert image description here

2.2 sed character replacement

sed 's/nologin/westos/g' passwd
sed '4,5s/nologin/westos/g' passwd
sed '$s/nologin/westos/g' passwd
sed '/lp/,/sssd/s/sbin/westos/g' passwd

sed 's/\//#####/g' passwd
 sed 's@/@#####@g' passwd     
sed 's@/@#####@g' -i passwd    //将更改的内容保存到源文件

Replace nologin in the full text with westos Replace nologin in
insert image description here
lines 4 and 5 with westos
insert image description here

Replace nologin in the last line with westos
insert image description hereReplace sbin between lp and sssd with westos
insert image description here

Replace / with #####
insert image description hereReplace / with #####
insert image description here

3.awk

insert image description herecharacter meaning
NO Number of lines
NF number of columns
FILENAME the file name itself
westos variable
Westos string

insert image description here
insert image description here
insert image description hereSet westos=1, each row westos+1 is processed, and the total value is finally output
insert image description here

awk -F : '/nologin$|{print $1,$7}' passwd
/bash$/      #条件

awk -F : '/nologin$|^root/{print $1,$7}' passwd
/条件1|条件2/    #条件1或者条件2

awk -F : '/nologin$/||/^root/{print $1,$7}' passwd
/条件1/||/条件2/  #条件1或者条件2

wk -F : '/bash$/&&/^root/{print $1,$7}' passwd
/条件1/&&/条件2/  #条件1并且条件2
$0#所有的列
$1#第一列
$2#第二列
$3#第三列

Show that the seventh column ends with bash.
insert image description here
After-school exercise: Command exercise: Count the number of users who can be switched by su in the system and whose home directory is not under /home
Ideas: Use the awk command to intercept /etc/passwd with: as the delimiter The sixth column in the file does not start with /home (/ needs to be translated; it cannot be directly written without the home keyword, there is a situation similar to the user's home directory being /tmp/home), and the line ending with bash or sh is correct. Intercept results to count

awk -F : 'BEGIN{N=0}$6!~/^\home/&&/bash$|sh$/{N++}END{print N}' /etc/passwd

Guess you like

Origin blog.csdn.net/weixin_46971894/article/details/119719601