sed
sed is a stream editor, a non-interactive editor, which processes a row language
processing, the line currently being processed is stored in a temporary buffer, called a "model space" (pattern space)
followed by sed command after the contents of the buffer handling, processing is complete, the contents of the buffer sent to the screen.
Then the next line, which is repeated until the end of the file.
File contents not changed, unless you use redirection to store the output.
sed to be used to automatically edit one or more documents; repeatedly simplify file; programs installed for the preparation of
Find sed increased modify delete CRUD
# Check
sed single row query
sed multi-line query (address range)
sed filtering
# Increase
sed one-way increase
sed multi-line increase
sed cases increase
# Delete
sed delete function and case
# Change
sed to replace text
sed variable substitution
sed backreferences
sed Swap Case
sed execute multiple statements
to obtain the file line number
1.sed syntax
sed [options] [sed command] [input file]
2.sed command execution process
3. Create a test file
cat >person.txt<<EOF 101,oldboy,CEO 102,zhangyao,CTO 103,Alex,COO 104,yy,CFO 105,feixue,CIO EOF
4. Query display a single line of text line p
p sed specified print mode spatial content - n-Sed option to cancel the default output [the root Xiaoming @ ~] # Sed ' 1P ' person.txt 101 , Oldboy, the CEO 101 , Oldboy, the CEO 102 , zhangyao, the CTO 103 , Alex, COO 104 , YY, the CFO 105 , feixue, the CIO [Xiaoming the root @ ~] # Sed ' 1P ' person.txt - n- 101 , Oldboy, the CEO # display file last line [the root Xiaoming @ ~] # Sed ' $ P ' Person. TXT - n- 105 , feixue, the CIO
5. Query the continuous multi-line text, a plurality of lines continuously displayed, come go '1,4p'
P no address range is specified in front, then default to match all lines
Numerical address # - Recommended line number # 2 , 4 the second line to the fourth line of the content, comprising the second and fourth rows [Xiaoming the root @ ~] # Sed ' 2,4p ' person.txt - n- 102 , zhangyao, the CTO 103 , Alex, COO 104 , YY, the CFO ## regular address range - Fuzzy, easy to find and more [the root Xiaoming @ ~] # Sed -n ' / Oldboy / P ' person.txt 101 , Oldboy, the CEO [Xiaoming the root @ ~] # Sed -n ' /o.*y/p ' person.txt 101 , Oldboy, the CEO [the root @ Xiaoming~] # Sed -n ' /o.*y/,/105/p ' person.txt 101 , Oldboy, the CEO 102 , zhangyao, the CTO 103 , Alex, COO 104 , YY, the CFO 105 , feixue, the CIO ## display the first row to the fourth row of this file [the root Xiaoming @ ~] # Sed -n ' 1,4p ' person.txt ## contain the line 101 from the display 104 to the line containing the [@ Xiaoming the root ~] # Sed -n ' / 101 /, / 104 / P ' person.txt 101 , Oldboy, the CEO 102 , zhangyao, the CTO 103 , Alex, COO 104,yy,CFO
sed query feature comparison grep
1. are based on units of
2 supports regular
3.sed can do to specify your search, grep does not work
6. Filter plurality of strings
-r sed options, support for extended regular expression (| ()) By default, sed only supports basic regular expressions. [Xiaoming the root @ ~] # egrep ' Oldboy | YY ' person.txt 101 , Oldboy, the CEO 104 , YY, the CFO [Xiaoming the root @ ~] # Sed -rn ' / Oldboy | YY / P ' person.txt 101 , Oldboy , CEO 104 , YY, CFO about regular character sed there must have " / " . / oldboy / Key: sed query a single line of text query multiple lines of text using the numeric address range sed ' 2,4p ' person.txt -n
7. query specifies multiple lines
with a semicolon
[root@xiaoming ~]# sed -n '1p;3p;5p' person.txt 101,oldboy,CEO 103,Alex,COO 105,feixue,CIO
8. Add a single line of text a, i
I want to append to the file person.txt 2 lines 106 , lidao, UFO 107 , Bingbing, CEO
方法一:cat cat >>person.txt <<EOF 106,lidao,UFO 107,bingbing,CEO EOF 方法二:echo [root@xiaoming ~]# echo "asda > asdas > asdas" asda asdas asdas [root@xiaoming ~]# echo "106,lidao,UFO\n107,bingbing,CEO" 106,lidao,UFO\n107,bingbing,CEO person.txt [root@xiaoming ~]# echo -e "106,lidao,UFO\n107,bingbing,CEO" 106,lidao,UFO 107,bingbing,CEO person.txt
Method three: sed command # 2 after the first row was added a new row, content oldboy.com [Xiaoming the root @ ~] # Sed ' . 2A oldboy.com ' person.txt 101 , Oldboy, the CEO 102 , zhangyao, the CTO Oldboy. COM 103 , Alex, COO [Xiaoming the root @ ~] # Sed ' 2i oldboy.com ' person.txt 101 , Oldboy, the CEO oldboy.com 102 , zhangyao, the CTO A the append additional i insert inserted
# Last line appended to line 2. [Xiaoming the root @ ~] # Sed ' $ 106 A, lidao, the UFO \ N107, Bingbing, the CEO ' person.txt 101 , Oldboy, the CEO 102 , zhangyao, the CTO 103 , Alex, COO 104 , YY, the CFO 105 , feixue, the CIO 106 , lidao, the UFO 107 , Bingbing, the CEO
9. Delete the file contents d
d sed command, delete text $ last line represents the file # delete the first line [the root Xiaoming @ ~] # Sed ' 1D ' person.txt 102 , zhangyao, the CTO 103 , Alex, COO 104 , YY, the CFO 105 , feixue, the CIO 106 , lidao, the UFO 107 , Bingbing, the CEO
# The file contents, but it does not include a first row Method a: [@ Xiaoming the root ~] # grep -v ' Oldboy ' person.txt Method Two: [@ Xiaoming the root ~] # Sed ' / Oldboy / D ' person.txt method three: [@ Xiaoming the root ~] # awk ' ! / Oldboy / ' person.txt
10. The modified file contents c
禁用SELINUX sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config sed -i "7c SELINU=disabledX" /etc/selinux/config sed -i "/^SELINU=/ SELINU=disabledX" /etc/selinux/config
11. Replace Text
's/old/new/g' 's@@@g' 's###g' s/\/etc/hosts/\/etc/hosts.bak/g s#/etc/hosts#/etc/hosts.bak#/g [root@xiaoming ~]# cat person.txt 101,oldboy,CEO 102,zhangyao,CTO 103,Alex,COO 104,yy,CFO 105,feixue,CIO [root@xiaoming ~]# sed 's#[0-9]#oldboy#g' person.txt oldboyoldboyoldboy,oldboy,CEO oldboyoldboyoldboy,zhangyao,CTO oldboyoldboyoldboy,Alex,COO oldboyoldboyoldboy, YY, the CFO oldboyoldboyoldboy, feixue, the CIO [Xiaoming the root @ ~] # Sed ' S # [0-9] G ## ' person.txt , Oldboy, the CEO , zhangyao, the CTO , Alex, COO , YY, the CFO , feixue, the CIO
Case: Remove 10 consecutive random string lowercase
openssl rand -base64 123 | sed 's#[^a-z]##g'| head -c 10
### - I automatic backup feature, back up, in the modified file contents [@ Xiaoming the root ~] # Sed -ri.bak ' S # [0-9] # # Oldboy ' person.txt [Xiaoming the root @ ~ ] # ll total amount of 24 -rw-R & lt - r-- . 1 the root the root 137 . 1 dated . 7 22 is : 14 person.txt -rw-R & lt - r-- . 1 the root the root 102 . 1 dated . 7 21 is : 22 is person.txt.bak