Linux Three Musketeers--sed
13.1 sed Introduction
- Stream Editor (sed--)
- SED features vs. version: processing text files, logs, configuration files
Add, delete, modify, query
Sed--version
- 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
- sed command execution process
13.2 SED Command Function example
- equals ' = ' displays line numbers sed = person.txt
- Single row-Enter line number to Sed-n ' 3p ' person.txt
103,alex,coo
- Displays multiple lines sed-n ' 3,6p ' person.txt
- Line Sed-n '/oldboy/p ' person.txt containing Oldboy
101,oldboy,ceo
- Show 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
- Show lines containing Oldboy or yy sed-nr '/oldboy|yy/p ' person.txt
- Show discontinuous lines Sed-nr ' 1p;3p;4p ' person.txt
- 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.txtcat >>person.txt <<eof
106,wuxingge,ufo
107,lee,xo
Eof
- Do not show blank line Method! means take anti sed '/^$/d ' person.txtsed-n '/^$/!p ' Person.txtawk '!/^$/' person.txtgrep-v ' ^$ ' person.txt
- ' s### ' means that the first object that meets the criteria is replaced by line one 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
- The & symbol in the SED command represents the contents of the previous regular match to the 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>
13.3 Batch modification of file names
Method 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 ' |bashMethod 2-Use the Rename method to bulk modify file names
Rename jpg avi old*.jpgRename from to File
Replace what replaces the content with what files
13.4 Common pits using SED commands
- -R and-I use simultaneously:
-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
- -R and-n are used simultaneously: the contents of the source file are emptied to show only eligible content
Linux Three Musketeers--sed command brief