sed use syntax
[Email protected] test]# sed--help
Usage: sed [options] ... {Script (if no other script)} [Input file] ...
-N,--quiet,--silent Cancel auto Print mode space
-e script,--expression= script adds "script" to the program's run list
-F script file,--file= script file Add "script file" to the program's run list
--follow-symlinks follow symlinks when processing on place; Hard links would still be broken.
-i[suffix],--in-place[=suffix]
Edit files in place (makes backup if extension supplied).
The default operation mode is to break symbolic and hard links.
This can is changed with--follow-symlinks and--copy.
-C,--copy
Use copy instead of rename when shuffling files In-i mode.
While this would avoid breaking links (symbolic or hard), the
Resulting editing operation is not atomic. This is rarely
the desired mode; --follow-symlinks is usually enough, and
It is both faster and more secure.
-L N,--line-length=n specifies the line length expected for the "L" command
--posix Close all GNU extensions
-R,--regexp-extended using extended regular expressions in scripts
-S,--separate the input file as individual files instead of a long continuous input
-U,--unbuffered reads the least data from the input file and refreshes the output more frequently
--help Print Help and exit
--version output version information and exit
Print a travel number and delete 2-5 rows
[Email protected] test]# NL/ETC/PASSWD | Sed ' 2,5d ' |more
1 Root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash
6 Sync:x:5:0:sync:/sbin:/bin/sync
7 Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 Halt:x:7:0:halt:/sbin:/sbin/halt
9 Mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
Ten Uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
Print travel number, delete only 2-5 rows
[Email protected] test]# NL/ETC/PASSWD | Sed ' 2d ' |more
1 Root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 Adm:x:3:4:adm:/var/adm:/sbin/nologin
5 Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
Print travel number, delete all content after line 2nd
[Email protected] test]# NL/ETC/PASSWD | Sed ' 2, $d '
1 Root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash
[Email protected] test]#
Print travel number and add "drink tea" after line 2nd
[Email protected] test]# nl/etc/passwd |sed ' 2a Drink tea ' | More
1 Root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash
2 Bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 Adm:x:3:4:adm:/var/adm:/sbin/nologin
Print travel number, add 2 lines "drink tea or drink beer"
[Email protected] test]# nl/etc/passwd |sed ' 2a drink tea or ... \
Drink beer? ' | More
1 Root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash
2 Bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or ...
Drink beer?
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin
4 Adm:x:3:4:adm:/var/adm:/sbin/nologin
Print a travel number and change the contents of the 2–5 line to "No 2-5"
[Email protected] test]# NL/ETC/PASSWD | Sed ' 2,5c No 2-5 number ' | More
1 Root:x:0:0:root,704,03738888766,03738888766:/root:/bin/bash
No 2-5 Number
6 Sync:x:5:0:sync:/sbin:/bin/sync
7 Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8 Halt:x:7:0:halt:/sbin:/sbin/halt
Print a travel number, listing only the contents of the 第5-7 line
[Email protected] test]# NL/ETC/PASSWD | Sed-n ' 5,7p '
5 Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 Sync:x:5:0:sync:/sbin:/bin/sync
7 Shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
An explanation of the use of SED in Linux (adding, deleting, etc.)