Linux Three Musketeers-sed

Sed's processing of text is very powerful, and sed is very small, with few parameters, and easy to master. Its operation method is a bit like awk. sed reads the file line by line in order. It then performs all the operations specified for the line, and after completing the requested modification, the contents are displayed, or can be placed in a file. After doing everything on a line, it reads the next line of the file and repeats the process until it finishes the file. One thing to note here, the source files (by default) remain unmodified. sed by default reads the entire file and modifies every line in it. To put it bluntly, it is a line-by-line operation. I use sed mainly to use the replacement function inside, which is really powerful. Let's take an example and talk about it in detail, starting with replacement, the most commonly used.

parameter:

copy code
copy code
1 sed -h
 2 -n, --quiet, --silent cancel automatic print mode space
 3 -e script, --expression=script adds "script" to the program's run list
 4 -f script file, --file=script file Add "script file" to the program's run list
 5 --follow-symlinks Follow soft links when modifying files directly
 6 -i[extension], --in-place[=extension] Modify the file directly (backup the file if the extension is specified)
 7 -l N, --line-length=N specify the expected length of newlines for the "l" command
 8 --posix turns off all GNU extensions
 9 -r, --regexp-extended use extended regular expressions in scripts
10 -s, --separate treat input files as separate files instead of one long continuous input
11 -u, --unbuffered Read minimal data from input file, flush output more frequently
12 --help print help and exit
13 --version output version information and exit
copy code
copy code

Example exercise:

Test files such as:

 

copy code
copy code
1 root:x:0:0:root:/root:/bin/bash
 2 bin:x:1:1:bin:/bin:/bin/false
 3 daemon:x:2:2:daemon:/sbin:/bin/false
 4 mail:x:8:12:mail:/var/spool/mail:/bin/false
 5 ftp:x:14:11:ftp:/home/ftp:/bin/false
 6 &nobody:$:99:99:nobody:/:/bin/false
 7 zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
 8 http:x:33:33::/srv/http:/bin/false
 9 dbus:x:81:81:System message bus:/:/bin/false
10 hal:x:82:82:HAL daemon:/:/bin/false
11 mysql:x:89:89::/var/lib/mysql:/bin/false
12 aaa:x:1001:1001::/home/aaa:/bin/bash
13 ba:x:1002:1002::/home/zhangy:/bin/bash
14 test:x:1003:1003::/home/test:/bin/bash
15 @zhangying:*:1004:1004::/home/test:/bin/bash
16 policykit:x:102:1005:Po
copy code
copy code

 

One: Replace the root in the test file with tankzhang

1 # sed 's/root/tankzhang/' test

Two: Use tankzhang to replace all the root in the file test, please pay attention to the letter g

1 # sed 's/root/tankzhang/g' test |grep zhang
2  tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash
3 zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
4  ba:x:1002:1002::/home/zhangy:/bin/bash
5  @zhangying:*:1004:1004::/home/test:/bin/bash

Three: After adding -np, it means that only those lines that have been replaced (partial replacement) are printed. In the above example, I did not add grep

1 # sed -n 's/root/tankzhang/p' test
2  tankzhang:x:0:0:root:/root:/bin/bash

Four:, between the second line and the eighth line, replace the line starting with zhang, replace it with ying, and display the replaced line

1 # cat test | sed -ne '2,8s/^zhang/ying/gp'
2  yingy:x:1000:100:,,,:/home/zhangy:/bin/bash

Five: When there are multiple commands to be executed, they can be separated by semicolons, and the separator can be customized, the default is /. The above example means that between the second line and the eighth line, replace the line starting with zhang and replace it with ying, and between 5 and 10, replace dbus with goodbay, and display the replaced line

1 # cat test | sed -n  '2,8s/^zhang/ying/gp;5,10s#dbus#goodbay#gp'
2  yingy:x:1000:100:,,,:/home/zhangy:/bin/bash
3  goodbay:x:81:81:System message bus:/:/bin/false

Six: , line replacement, replace the line matching zhangy with the line matching root

copy code
copy code
1 $ sed -e '/root/h' -e '/zhangy/g' test
 2  root:x:0:0:root:/root:/bin/bash
 3  bin:x:1:1:bin:/bin:/bin/false
 4  daemon:x:2:2:daemon:/sbin:/bin/false
 5  mail:x:8:12:mail:/var/spool/mail:/bin/false
 6  ftp:x:14:11:ftp:/home/ftp:/bin/false
 7  &nobody:$:99:99:nobody:/:/bin/false
 8  root:x:0:0:root:/root:/bin/bash
 9  http:x:33:33::/srv/http:/bin/false
10  dbus:x:81:81:System message bus:/:/bin/false
11  hal:x:82:82:HAL daemon:/:/bin/false
12  mysql:x:89:89::/var/lib/mysql:/bin/false
13  aaa:x:1001:1001::/home/aaa:/bin/bash
14  root:x:0:0:root:/root:/bin/bash
15  test:x:1003:1003::/home/test:/bin/bash
16  root:x:0:0:root:/root:/bin/bash
copy code
copy code

Note: special match

copy code
copy code
1 To match numbers, don't forget that there is a bracket outside the brackets.
 2 [:alnum:] Alphanumeric [az AZ 0-9]
 3 [:alpha:] letters [az AZ]
 4 [:blank:] space or tab
 5 [:cntrl:] any control character
 6 [:digit:] digit[0-9]
 7 [:graph:] Any visual character (no spaces)
 8 [:lower:] lowercase [az]
 9 [:print:] non-control characters
10 [:punct:] Punctuation characters
11 [:space:] space
12 [:upper:] uppercase [AZ]
13 [:xdigit:] Hex digits [0-9 af AF]
copy code
copy code

Example a, delete lines 1 and 14

1 $ sed -e '1,14d' test
2  @zhangying:*:1004:1004::/home/test:/bin/bash
3  policykit:x:102:1005:Po

Example b, delete the lines after 4, including the 4th line, and use $ as the maximum number of lines.

1 $ sed -e '4,$d' test
2  root:x:0:0:root:/root:/bin/bash
3  bin:x:1:1:bin:/bin:/bin/false
4  daemon:x:2:2:daemon:/sbin:/bin/false

Example c, delete the line including false, or the line including bash, don't forget to add \

1 $ sed -e '/ \ (false \ | bash \) $ / d' test
2  policykit:x:102:1005:Po

Example d, delete from the line matching root to the line starting with test, and the middle line

1 $ sed -e '/root/,/^test/d' test
2  @zhangying:*:1004:1004::/home/test:/bin/bash
3  policykit:x:102:1005:Po
refer to:
https://www.cnblogs.com/guigujun/p/6221193.html

Sed's processing of text is very powerful, and sed is very small, with few parameters, and easy to master. Its operation method is a bit like awk. sed reads the file line by line in order. It then performs all the operations specified for the line, and after completing the requested modification, the contents are displayed, or can be placed in a file. After doing everything on a line, it reads the next line of the file and repeats the process until it finishes the file. One thing to note here, the source files (by default) remain unmodified. sed by default reads the entire file and modifies every line in it. To put it bluntly, it is a line-by-line operation. I use sed mainly to use the replacement function inside, which is really powerful. Let's take an example and talk about it in detail, starting with replacement, the most commonly used.

parameter:

copy code
copy code
1 sed -h
 2 -n, --quiet, --silent cancel automatic print mode space
 3 -e script, --expression=script adds "script" to the program's run list
 4 -f script file, --file=script file Add "script file" to the program's run list
 5 --follow-symlinks Follow soft links when modifying files directly
 6 -i[extension], --in-place[=extension] Modify the file directly (backup the file if the extension is specified)
 7 -l N, --line-length=N specify the expected length of newlines for the "l" command
 8 --posix turns off all GNU extensions
 9 -r, --regexp-extended use extended regular expressions in scripts
10 -s, --separate treat input files as separate files instead of one long continuous input
11 -u, --unbuffered Read minimal data from input file, flush output more frequently
12 --help print help and exit
13 --version output version information and exit
copy code
copy code

Example exercise:

Test files such as:

 

copy code
copy code
1 root:x:0:0:root:/root:/bin/bash
 2 bin:x:1:1:bin:/bin:/bin/false
 3 daemon:x:2:2:daemon:/sbin:/bin/false
 4 mail:x:8:12:mail:/var/spool/mail:/bin/false
 5 ftp:x:14:11:ftp:/home/ftp:/bin/false
 6 &nobody:$:99:99:nobody:/:/bin/false
 7 zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
 8 http:x:33:33::/srv/http:/bin/false
 9 dbus:x:81:81:System message bus:/:/bin/false
10 hal:x:82:82:HAL daemon:/:/bin/false
11 mysql:x:89:89::/var/lib/mysql:/bin/false
12 aaa:x:1001:1001::/home/aaa:/bin/bash
13 ba:x:1002:1002::/home/zhangy:/bin/bash
14 test:x:1003:1003::/home/test:/bin/bash
15 @zhangying:*:1004:1004::/home/test:/bin/bash
16 policykit:x:102:1005:Po
copy code
copy code

 

One: Replace the root in the test file with tankzhang

1 # sed 's/root/tankzhang/' test

Two: Use tankzhang to replace all the root in the file test, please pay attention to the letter g

1 # sed 's/root/tankzhang/g' test |grep zhang
2  tankzhang:x:0:0:tankzhang:/tankzhang:/bin/bash
3 zhangy:x:1000:100:,,,:/home/zhangy:/bin/bash
4  ba:x:1002:1002::/home/zhangy:/bin/bash
5  @zhangying:*:1004:1004::/home/test:/bin/bash

Three: After adding -np, it means that only those lines that have been replaced (partial replacement) are printed. In the above example, I did not add grep

1 # sed -n 's/root/tankzhang/p' test
2  tankzhang:x:0:0:root:/root:/bin/bash

Four:, between the second line and the eighth line, replace the line starting with zhang, replace it with ying, and display the replaced line

1 # cat test | sed -ne '2,8s/^zhang/ying/gp'
2  yingy:x:1000:100:,,,:/home/zhangy:/bin/bash

Five: When there are multiple commands to be executed, they can be separated by semicolons, and the separator can be customized, the default is /. The above example means that between the second line and the eighth line, replace the line starting with zhang and replace it with ying, and between 5 and 10, replace dbus with goodbay, and display the replaced line

1 # cat test | sed -n  '2,8s/^zhang/ying/gp;5,10s#dbus#goodbay#gp'
2  yingy:x:1000:100:,,,:/home/zhangy:/bin/bash
3  goodbay:x:81:81:System message bus:/:/bin/false

Six: , line replacement, replace the line matching zhangy with the line matching root

copy code
copy code
1 $ sed -e '/root/h' -e '/zhangy/g' test
 2  root:x:0:0:root:/root:/bin/bash
 3  bin:x:1:1:bin:/bin:/bin/false
 4  daemon:x:2:2:daemon:/sbin:/bin/false
 5  mail:x:8:12:mail:/var/spool/mail:/bin/false
 6  ftp:x:14:11:ftp:/home/ftp:/bin/false
 7  &nobody:$:99:99:nobody:/:/bin/false
 8  root:x:0:0:root:/root:/bin/bash
 9  http:x:33:33::/srv/http:/bin/false
10  dbus:x:81:81:System message bus:/:/bin/false
11  hal:x:82:82:HAL daemon:/:/bin/false
12  mysql:x:89:89::/var/lib/mysql:/bin/false
13  aaa:x:1001:1001::/home/aaa:/bin/bash
14  root:x:0:0:root:/root:/bin/bash
15  test:x:1003:1003::/home/test:/bin/bash
16  root:x:0:0:root:/root:/bin/bash
copy code
copy code

Note: special match

copy code
copy code
1 To match numbers, don't forget that there is a bracket outside the brackets.
 2 [:alnum:] Alphanumeric [az AZ 0-9]
 3 [:alpha:] letters [az AZ]
 4 [:blank:] space or tab
 5 [:cntrl:] any control character
 6 [:digit:] digit[0-9]
 7 [:graph:] Any visual character (no spaces)
 8 [:lower:] lowercase [az]
 9 [:print:] non-control characters
10 [:punct:] Punctuation characters
11 [:space:] space
12 [:upper:] uppercase [AZ]
13 [:xdigit:] Hex digits [0-9 af AF]
copy code
copy code

Example a, delete lines 1 and 14

1 $ sed -e '1,14d' test
2  @zhangying:*:1004:1004::/home/test:/bin/bash
3  policykit:x:102:1005:Po

Example b, delete the lines after 4, including the 4th line, and use $ as the maximum number of lines.

1 $ sed -e '4,$d' test
2  root:x:0:0:root:/root:/bin/bash
3  bin:x:1:1:bin:/bin:/bin/false
4  daemon:x:2:2:daemon:/sbin:/bin/false

Example c, delete the line including false, or the line including bash, don't forget to add \

1 $ sed -e '/ \ (false \ | bash \) $ / d' test
2  policykit:x:102:1005:Po

Example d, delete from the line matching root to the line starting with test, and the middle line

1 $ sed -e '/root/,/^test/d' test
2  @zhangying:*:1004:1004::/home/test:/bin/bash
3  policykit:x:102:1005:Po

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325228691&siteId=291194637