1th SED Foundation 1.1 sed Option Stream Editor
-R supports extended regular
-N cancels the default output
-I direct modification does not output to the desktop
-i.bak Backup
The writing format of 1.2 sed
Sed ' conditional command '
Sed ' pattern command '
Sed ' mode command '
1.3 sed command execution process
1. Read the contents of the file 1th line
2. Whether the conditions are met
1). The command that satisfies the condition executes the corresponding P s D
2). Not satisfied continue to 1th step
3. To the last line of the file
Chapter 2nd use of the SED command
Sed add and delete to change the no I option will only change the contents of the output to not really modify, plus I option will directly modify the file content
2.1 Sed increased CAI
C Replace replacement
A append Append
I insert Inserts
2.1.1 Append 12306,xiao,ufo to the third line of the Person.txt file
View Person.txt File
[[email protected] LX] # Cat Person.txt101, Zeq,ceo102, Zhang,cto103, Love,coo104, YY,CFO105 , Faker,cio110,ahao,coco
Append 12306,xiao,ufo to the third line
[[email protected] LX] # sed ' 3a12306,xiao,ufo ' person.txt a Append, 3a is appended to the third line 101, Zeq,ceo102, Zhang,cto103, Love,coo12306, Xiao,ufo104, YY,CFO105 , Faker,cio110,ahao,coco
2.1.2 inserting 12306,xiao,ufo on the third line of the Person.txt file
[[email protected] LX] # sed ' 3i12306,xiao,ufo ' person.txt101, Zeq,ceo102, Zhang,cto12306, Xiao,ufo103 , Love,coo104, YY,CFO,Faker,cio110,ahao,coco
2.1.3 Replace the third line of the Person.txt file with 12306,xiao,ufo
[[email protected] LX] # sed ' 3c12306,xiao,ufo ' person.txt101, Zeq,ceo102, Zhang,cto12306, Xiao,ufo 104, YY,CFO, Faker,cio110,ahao,coco
2.1.4 added in last line of file
12306,xiao,ufo
12580,tao,xo
[[email protected] LX] # sed ' $a 12306,xiao,ufo\n12580,tao,xo ' person.txt $ last line \ n Enter 101, Zeq,ceo102, Zhang,cto103, Love,coo104, YY,CFO,Faker,cio, Ahao,coco12306 , Xiao,ufo12580,tao,xo
2.2 sed Delete d 2.2.1 Delete Row 2nd to line 5th
[[email protected] LX] # sed ' 2,5d ' person.txt101, Zeq,ceo110,ahao,coco
2.2.2 Delete the line from YY to the end of the file
[[email protected] LX] # sed '/yy/, $d ' person.txt $ file End 101, Zeq,ceo102, Zhang,cto103,love,coo
2.2.3 Delete rows that contain Ahao
[[email protected] LX] # sed '/ahao/d ' person.txt101, Zeq,ceo102,zhang,cto103, Love,coo104, YY, CFO105,faker,cio
2.2.4 Delete rows that do not contain Ahao
[[email protected] LX] # sed '/ahao/!d ' person.txt take the reverse, exclude 110,ahao,coco
2.3 sed replaces ' s## #g ' 2.3.1 Replace all numbers
[[email protected] LX] # sed ' s#[0-9]# #g ' person.txt, Zeq,ceo,zhang,cto,love,coo,yy,cfo,faker,cio,ahao,coco
2.3.2 Replace the first digit in each line
[[email protected] LX] # sed ' s#[0-9]## ' person.txt sed ' s#[0-9]## ' without G, default is match to the first digit of each line sed ' s#[0-9]## ' matches the second digit of each line zeq,ceo,zhang,cto,love,coo,yy,cfo,Faker, CIO10,ahao,coco
2.3.3 The IP address after the reference is taken
[[Email protected] ~] # ifconfig eth0 |sed-rn ' 2s#^.*r: (. *) BC.*#\1#GP '10.0.0.200
[[Email protected] ~] # ifconfig eth0 |awk-f ' [:]+ ' Nr==2{print $4} '10.0.0.200
[[Email protected] ~] # ifconfig eth0 |awk-f ' addr:| Bc ' nr==2{print $ '10.0.0.200
[[Email protected] ~] # ifconfig eth0 |awk-f ' [^0-9.] + ' nr==2{print $ '10.0.0.200
2.4 sed 2.4.1 Display the 5th line of the file
[[Email protected] ~] # sed-n 5p Person.txt105,faker,cio
2.4.2 Show the file line 2nd to line 5th
[[Email protected] ~] # sed-n ' 2,5p ' person.txt102, Zhang,cto103, Love,coo104, YY,CFO105,faker,cio
2.4.3 display the last line of the file
[[Email protected] ~] # sed-n ' $p ' person.txt110,ahao,coco
2.4.4 displays the lines in the file that contain Zeq
[[Email protected] ~] # sed-n '/zeq/p ' person.txt101,zeq,ceo
2.4.5 display a line from a file that contains 101 lines to 105
[[Email protected] ~] # sed-n '/101/,/105/p ' person.txt101, Zeq,ceo102,zhang,cto103, Love,coo104, YY,CFO105,faker,cio
2.4.6 Special wording: Displays the 1th and 4 lines and 5 lines of the file
[[Email protected] ~] # sed-n ' 1p; 4p; 5p ' person.txt101, Zeq,ceo104, YY,CFO105,faker,cio
2.4.7 Show a regular line
[[Email protected] ~] # seq |sed-n ' 1~2p '13579
Using variables in 2.5 sed commands
[email protected] ~]# X=zeq set x variable to Zeq~]# y=zeq1 Set y variable to zeq1~]# sed ' s# $x # $y #g ' person.txt101, Zeq1,ceo102 , Zhang,cto103, Love,coo104, YY,CFO,Faker,cio, Ahao,coco 12306,xiao. Ufo
2.5.1 Replace the contents of each line in the file Person.txt file with the corresponding line number
for inch {1..7} The for loop in the shell script used here does"${n}s#.*# $n #g" person.txtdone
2.5.2 Batch rename: Delete html_ in file name
Touch zeq_html_{01..10}.jpg
Method 1 command-line format [[email protected] data]# ls *.jpg|sed-r "s# (. *) (_.*) #mv & Zeq\2#g "|bash
Method 2 For Loop for inch {01..10}domv zeq_${i}.jpg zeq_html_${i}.jpg Done
Method 3 renamerename html_ "" *.jpg rename modify who Change what file to change what
linux-sed command