1.1 sedIntroduction
1. Sed-stream editor
2. Sed features and versions:
Working with text files, logs, configuration files
Add, delete, modify, query
Sed--version
3. Syntax format:sed [ options ] [sed directive ] [ input file ]
Sed-i.bak ' S#boy#girl#g ' oldboy.txt
parameters of the- i sed command
s sed command
G Small tail / decoration
1.2 sedCommand Function Example
1. equals '=' Displays line numbers
# sed = person.txt
2. Single row-Enter line number
# sed-n ' 3p ' Person.txt
103,alex,coo
3. display multiple lines
# sed-n ' 3,6p ' person.txt
4. rows that contain Oldboy
# sed-n '/oldboy/p ' person.txt
101,oldboy,ceo
5. display lines containing Oldboy to display yy
# sed-n '/oldboy/,/yy/p ' person.txt
101,oldboy,ceo
102,zhangyao,cto
103,alex,coo
104,yy,cfo
6. display lines that contain Oldboy or yy
# sed-nr '/oldboy|yy/p ' person.txt
7. display discontinuous rows
# sed-nr ' 1p;3p;4p ' person.txt
8. Add two lines to the last line of the file
# sed ' $a 106,wuxingge,ufo\n107,lee,xo ' person.txt
#a ==append Append
#i ==insert Insertion
# echo-e ' 106,wuxingge,ufo\n107,lee,xo ' >>person.txt
# Cat >>person.txt <<eof
> 106,wuxingge,ufo
> 107,lee,xo
>eof
9. do not show blank line method ! represents an inverse
# sed '/^$/d ' person.txt
# sed-n '/^$/!p ' person.txt
# awk '!/^$/' person.txt
# grep-v ' ^$ ' person.txt
's###' means replacing the first object that meets the criteria by line
# sed ' s#1#dou# ' person.txt
Dou01,oldboy,ceo
-I means OK to modify
-i.bak means to back up and modify first
Double quotes inside the SED variable can be executed
A. sed command & symbol indicates what was previously matched to
# echo ' 123456 ' |sed ' s#.*#<&> #g '
<123456>
# echo ' 123456 ' |sed-r ' s# ([0-9]) #<\1> #g '
<1><2><3><4><5><6>
# echo ' 123456 ' |sed ' s#.#<&> #g '
<1><2><3><4><5><6>
1.3Batch Modify file name
1. Replace with sed
# ls *.jpg|sed-r ' s# (. *) JPG#MV & \1avi#g '
MV Oldboy01.jpg Oldboy01.avi
MV Oldboy02.jpg Oldboy02.avi
# ls *.jpg|sed-r ' s# (. *) JPG#MV & \1avi#g ' |bash
2. use The rename method to bulk modify file names
# rename jpg avi old*.jpg
Rename from to File
Replace what replaces the content with what files
1.4UseSedCommand Common pits
1. -R is used in conjunction with -I:
-r parameter in front:-ri Normal result
The-i parameter is in front:-ir indicates that the file is backed up XXR before replacing the contents of the file
2. - R and -N are used simultaneously: the contents of the source file will be emptied to show only eligible content
1.5 sedCommand Summary diagram
Linux Three Musketeers--sed