The usage of regular sed/awk
* sed can implement some functions of grep, but it is a little troublesome. The strength of sed is to delete the contents of the file and replace it.
Sed implements the grep retrieval function:
1. Search by keyword:
[root@localhost ~] # sed-n'/ root/'p passwd.txt
* when using sed to retrieve, you need to add the-n parameter before the keyword, p after the keyword, and / / include the keyword.
two。 Add the-r parameter or use the escape character when the keyword has a special symbol
[root@localhost ~] # sed-nr'/ o+t/'p passwd.txt
3. Print the specified line: (you can write the line number directly when printing the specified line, without the need to be enclosed by / / symbols)
[root@localhost ~] # sed-n '5compp passwd.txt [root@localhost ~] # sed-n' 5preceptionp passwd.txt # print the fifth line to the last line
4. Mure parameter: use multiple expressions:
[root@localhost] # sed-e '1roomp-e' / root/'p-n passwd.txt
* print the first line and retrieve the line containing root. If the first line also contains root, the first line will be printed twice.
5. Case-insensitive: (plus uppercase I)
[root@localhost ~] # sed-n'/ testword/'Ip passwd.txt
Sed deletion function:
1. Delete the line specified in the print result:
[root@localhost ~] # wc-l passwd.txt 22 passwd.txt# View the number of lines of files [root@localhost ~] # sed'1 wc'1 passwd.txt chrony:x:998:996::/var/lib/chrony:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash# delete lines 1-20, print the remaining lines [root@localhost ~] # wc-l passwd.txt 22 passwd.txt# this operation does not actually delete the contents of the file, just deletes the print result
2. Muri parameter: delete the specified line in the file (the file content will be deleted by adding the-I parameter)
[root@localhost] # wc-l passwd.txt 22 passwd.txt [root@localhost] # sed-I'1 wc 20 passwd.txt [root@localhost] # wc-l passwd.txt 2 passwd.txt
* used more commonly when deleting the contents of large log files
Sed replacement function:
1. Replace according to keyword:
[root@localhost ~] # cat passwd.txt chrony:x:998:996::/var/lib/chrony:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash [root@localhost ~] # sed's Universe synchronous passwd.txt sed_test:x:998:996::/var/lib/sed_test:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash
* format:'s / replaced keywords / replaced content / g'
two。 Add the-r parameter when matching special symbols:
[root@localhost ~] # cat passwd.txt nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash [root@localhost ~] # sed-r's go after nasty Universe G'passwd.txt sed_test:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash
3. Segment replacement location:
[root@localhost ~] # cat passwd.txt nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash [root@localhost ~] # sed-r's / ([^:] +): (. *): ([^:] +) /\ 3:\ 2:\ 1Universe g'passwd.txt / sbin/nologin:x:998:996::/var/lib/chronwy:nnnnny/bin/bash : x:1000:1000::/home/linux01:linux01
* use colons to divide into 3 segments and swap the positions of the third and first segments
4. When you configure the keyword to be a directory path, add an escape character or use another alternative symbol:
[root@localhost ~] # cat passwd.txt nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash [root@localhost ~] # sed's /\ / bin\ / bash/AAAAAAA/g' passwd.txt nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:AAAAAAA [root@localhost ~] # sed 's#/bin / bash#AAAAAAA#g' passwd.txt nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:AAAAAAA
5. Delete all letters:
[root@localhost ~] # cat passwd.txt nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash [root@localhost ~] # sed's / [a-zA-Z] / / g 'passwd.txt:: 998, 996, 100, 000, 100, 000, 100, 000, 100, 100, 100, 100, 100, 100, 100, 100, 100, 000, 100, 000, 100, 000, 100, 000, 100, 000, 100, 000, 100, 000, 100, 000, 100, 000, 100, 000, 000, 100, 000, 100, 000, 100, 000, 100, 000, 000, 100, 000,
6. Add content before each line:
[root@localhost ~] # cat passwd.txt nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash [root@localhost ~] # sed-r's passwd.txt sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinsed_test:linux01:x:1000:1000::/home/linux01:/bin/bash.
* match all the contents of each line:. *, & symbol represents the content of. *
7. The contents of the actual replacement file:-I parameter (none of the above 6 examples will actually change the contents of the file, only the printout result of the replacement)
[root@localhost ~] # cat passwd.txt nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinlinux01:x:1000:1000::/home/linux01:/bin/bash [root@localhost ~] # sed-I's Unip. G'passwd.txt [root@localhost ~] # cat passwd.txt sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinsed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
Add:
Capitalize the first lowercase letter of each word:
Sed's /\ b [a-z] /\ uplink G'filename
Change all lowercase to upper case:
Sed's / [a Meiz] /\ uplink Universe g'filename
Uppercase to lowercase:
Sed's / [Amurz] /\ lhammer G'filename
Sed adds a number at the end of a line
Sed-r's / (^ a.*) /\ 1 12max 'test
Sed-r's / ^ a. * / & 12 Universe 'test
Print 1 to 100 lines containing a string
Sed-n '1100 {/ abc/p}' 1.txt
* awk is more powerful than grep/egrep/sed. It supports extending regular expressions by default. Grep needs to add-E parameter, and sed needs to add-r parameter.
1. Split the contents of the file to print the specified number of segments:
[root@localhost ~] # cat test.txt zhangsan 100lisi 92wangwu 95user1 88user2 93 [root@localhost ~] # awk'{print $1} 'test.txt zhangsanlisiwangwuuser1user2
* spaces are used as delimiters by default, and $specifies the number of segments to be printed.
2.After parameter: specify the delimiter:
[root@localhost ~] # cat passwd.txt AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'{print $3} 'passwd.txt nnnnnylinux01# use a comma to distinguish [root@localhost ~] # awk-F':'{print $1 $3} 'passwd.txt AAAA nnnnnyAAAA linux01# specifies the split symbol [root@localhost ~] # awk-F':'{print $1 "- >" $3} 'passwd.txt AAAA-- > nnnnnyAAAA-- > linux01 in the middle of the print result
* awk {print $0} means to print all, which is equivalent to cat
3.awk search: (equivalent to grep)
[root@localhost ~] # cat passwd.txt AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk'/ nnn/' passwd.txt AAAA:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologin
4. Retrieve the lines that contain keywords in the specified segment:
[root@localhost ~] # cat passwd.txt ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'$1 ~ / AAA/' passwd.txt AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
5. Multiple expressions are combined:
[root@localhost ~] # cat passwd.txt ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'/ linux/ {print $1 passwd.txt sed_test nnnnnyAAAA linux01 3} / nnn/ {root @ root $2
* print paragraphs 1 and 3 of the line containing the linux keyword, and 2 and 3 paragraphs containing the nnn keyword
6. Retrieve lines for multiple keywords and specify print segments:
[root@localhost ~] # cat passwd.txt ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'/ linux | nnn/ {print $1} 'passwd.txt ABCDAAAA
7. To retrieve through operational symbols:
[root@localhost ~] # cat passwd.txt ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'$5 million 1 000 passwd.txt AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'$5 million 1 000 {print $1} 'passwd.txt AAAA
8. When judging a number, do not add double quotation marks, otherwise the judgment condition will be treated as a string rather than a number (when a number is judged as a string, according to the ASCII code, 998 is greater than 1000):
[root@localhost ~] # cat passwd.txt ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'$5 $6' passwd.txt ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/ science [root @ localhost ~] # awk-F':'$5 > $6 {print $5 6} 'passwd.txt 998 996 [root@localhost ~] # awk-F':'$5 > 999' passwd.txt AAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'$5 > 900 & & $6999 | $9 million = "/ sbin/nologin" 'passwd.txt ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/bash
* the last string judgment can be retrieved in addition to the = = symbol.
11.OFS: specify the print result delimiter:
[root@localhost ~] # cat passwd.txt ABCD:sed_test:nnnnny:x:998:996::/var/lib/chronwy:/sbin/nologinAAAA:sed_test:linux01:x:1000:1000::/home/linux01:/bin/ bash [root @ localhost ~] # awk-F':'{OFS= "* *"} {print $1Bash 2 $3} 'passwd.txt ABCD***sed_test***nnnnnyAAAA***sed_test*** Linux01 [root @ localhost ~] # awk-F':'{OFS= "* *"} $5