6.2 bash programming Integer testing and Special variables
6.2 The three great Linux tools.
grep,sed(stream editor), awk
sed basic usage
sed :Stream EDitor
Line Editor (Full Screen Editor: vi)
sed: pattern space
By default, the original file is not edited, only the data in the pattern space is processed: then, processing
When finished, print pattern space to screen
sed [options] 'AddressCommand' file ...
-n: Silent mode, no longer the default display mode space content
Address:
1 StartLine,EndLine
For example 1, 100 (lines 1 to 100)
$: Last line
2 /REGEXP/
For example/^root/
sed '/oot/d' /etc/fstab
3 /Pattern1/,/Pattern2/
Indicates the beginning of the row first matched by Pattern1, to the first matched by Pattern2
Match to the end of the row, all the rows in between
4 LineNumber
the specified row
sed '1d' /etc/fstab
5 StartLine,+N
Starting from StartLine, N lines backward, N+1 lines in total
sed '1,+2d' /etc/fstab
Command:
d: Delete eligible rows
p: Display eligible rows
a \string: appends a new line after the specified line with the content "string"
\n: Can be used for newlines
i \string: appends a new line with the content "string" before the specified line
r FILE: Adds the contents of the specified file to the qualifying line
w FILE: Save lines in the specified range of addresses as specified files
s/pattern/string/modifier: find and replace (default only replaces the first time in each line
String matched by pattern)
modifier
g: Global substitution
i: ignore case
sed '1,2d' /etc/fstab
sed '/^\//d' /etc/fstab
sed '/^\//a \# hello world' /etc/fstab
sed '/^\//a \# hello world\n # hello,linux' /etc/fstab
sed '2r /etc/issue' /etc/fstab
sed '/oot/w /tmp/oot.txt' /etc/fstab
sed 's/oot/OOT/' /etc/fstab
sed 's/^\//#/' /etc/fstab