Linux Shell notes of sed

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
Sed: stream editor, stream editor
The SED editor itself does not modify the text file data, will only send the modified data to STDOUT
The command type: sed options script file

A, sed
The 1 definition editor

# echo "this is a test" | sed 's/test/sed test/' //Replace test with sed test
this is a sed test

2 simultaneous use of multiple editor
# echo "this is a test" | sed -e 's/test/sed test/;s/this/it/'
it is a sed test
Execution of a plurality of command, use the -e option, the command with a semicolon

3 read from file editor commands
# cat sedtest1
s/this/it/
s/test/sed test/
# cat data1
this is a test
# sed -f sedtest1 data1 //Use the -f option to specify the file
it is a sed test

4.sed replacement options
s/pattern/replacement/flags
1)Digital replacement tag
# cat cat
it is cat cat
it is cat cat
# sed 's/cat/dog/2' cat //Replace the matching pattern appeared second times in a row
it is cat dog
it is cat dog

2)g,The global replacement tag
# sed 's/cat/dog/g' cat //Replace all
it is dog dog
it is dog dog

3)p,The original line must be typed.
# sed -n 's/cat/dog/p' cat //-n no sed editor output, P output only modified lines, both with the only output the modified line
it is dog cat
it is dog cat

4)w,Save the replacement results to file
# sed 's/cat/dog/w test' cat
it is dog cat
it is dog cat

5)Replacement character
# sed 's!/bin/bash!/bin/csh!' passwd //Use! Number as string delimiter, makes the path name is easier to read and understand
root:x:0:0:root:/root:/bin/csh

5.sed address
The SED editor will work for all default text data, to specify a row or rows, need to use the row addressing
# cat cat
it is cat cat
it is cat cat
it is cat 3
it is cat 4

1)Addressing the digital way
# sed '2s/cat/dog/' cat //Replace the matching pattern in the second row
it is cat cat
it is dog cat
# sed '2,3s/cat/dog/' cat //The replacement of second, line three
it is cat cat
it is dog cat
it is dog 3
# sed '2,$s/cat/dog/' cat //$At the end of the code symbol, the replacement of two to the end of the
it is cat cat
it is dog cat
it is dog 3
it is dog 4

2)The text mode filter
# sed '/3/s/cat/dog/' cat //The use of regular expressions, modify only contains "3".
it is cat cat
it is cat cat
it is dog 3

6 delete rows
# sed 'd' cat //Don't do any match, D will delete all
# sed '3d' cat //Delete line third
it is cat cat
it is cat cat
it is cat 4

# sed '/4/d' cat //Delete rows containing "4"
it is cat cat
it is cat cat
it is cat 3

7 insertion and additional text
1)The command I to add a new line in the specified command line, a in the specified row added a new line
# echo "it is line 2"| sed 'i\it is line 1' //I is the right \ symbol
it is line 1
it is line 2

# echo "it is line 2"| sed 'a\it is line 1'
it is line 2
it is line 1

2)The specified line insertion or additional lines
# cat dataline
line 1
line 2
line 3
# sed '1i\ //I with digital, designated the first few lines
> it is line 1\ //Need to add the backslash at the beginning of each row insert rows
> it is line 2' dataline
it is line 1
it is line 2
line 1
line 2
line 3

# sed '1a\
> it is line one\
> it is line two' dataline
line 1
it is line one
it is line two
line 2
line 3

The 8 amendment, mechanism with insertion and additional
# sed '1c\it is line one' dataline
it is line one
line 2
line 3

The 9 conversion command
# sed 'y/123/789/' dataline //The Y command conversion, 123 are converted to 789, should be the same length
line 7
line 8
line 9

The 10 print
1)Print line
# sed -n '2,3p' dataline
line 2
line 3

2)Print line numbers
# sed '=' dataline //=In order to print line numbers
1
line 1
2
line 2
3
line 3

3)Train travel
# cat datatab
this line contains tabs
# sed -n 'l' datatab //The L command (list) can display tab special characters
this\tline\tcontains\ttabs$

11.sed document processing
1)Written to the file, type : [address]w filename
]# sed '1,2w test' dataline //The w command to write data to a file, and -n can't output to the console information
line 1
line 2
line 3
# cat test
line 1
line 2

2)Read data from a file
# cat dataadd
it is added line one
it is added line two
# sed '3r dataadd' dataline //The R command can be a file in the data into the data stream
line 1
line 2
line 3
it is added line one
it is added line two

Two, advanced sed
The pattern space (pattern space): is a buffer, the text in the SED editor to execute a command it will save the SED editor to test
Keep the space (hold space): another piece of the buffer area, can other row in pattern space holding space comes to save some for
The SED editor will start at the top of the script to execute the command and process to the end of the script (D command exception)
The 1.next command
1)Single line next command
# cat dataheader
this is the header line

this is the first data line
this is the second data line

this is the last line
# sed '/header/{n;d}' dataheader //The N command in the SED editor to move to the next text line data stream
this is the header line //Find the header line, moving on to the next line, delete
this is the first data line
this is the second data line

this is the last line

2)With text line
# sed '/first/{N;s/\n//}' dataheader //The N command will the next article we add it to the text in the pattern space.
this is the header line

this is the first data line this is the second data line

this is the last line

More than 2 lines of the delete command
# cat dataheader

this is the header line

this is the first data line
this is the second data line
# sed '/^$/{N;/header/D}' dataheader //The D command to delete only the first row in the pattern space
this is the header line //Find a blank line, moving on to the next line, if the matching header, remove the first line using the D command

this is the first data line
this is the second data line

More than 3 lines of print command
# cat dataroot
the first meeting of the root
user group will be held on Tuesday.
All root user should attend this meeting
# sed -n 'N;/root\nuser/P' dataroot //The P command will only be the first line of print in the pattern space
the first meeting of the root

4 keep the space
H will be copied to maintain spatial pattern space
H the model space added to keep the space
G will keep the space to copy the pattern space
G will keep the space added to the pattern space
X switching mode space and keep the space content
# cat dataheader
this is the header line
this is the first data line
this is the second data line
# sed -n '/first/{
> h //The first row is copied into the holding space
> n //Reads the next line, and put it in the pattern space
> p //To print the pattern space data, which are second rows
> g //Will keep the space to copy the pattern space
> p //To print the pattern space data, that is the first line
> }' dataheader
this is the second data line
this is the first data line

5 exclusion orders
!The exclamation mark command to exclusion orders, that would play a role in the command does not work
# sed -n '/header/!p' dataheader //In addition to header, other do not print.
this is the first data line
this is the second data line

# sed -n '1!G;h;$p' dataheader //Inversion data, 1! G, the first line, not will keep the space added to model space
this is the last line

this is the second data line
this is the first data line
this is the header line

# tac dataheader //The TAC command to execute the cat command the reverse function
this is the last line

this is the second data line
this is the first data line
this is the header line

6 change the flow
1)Jump.(branch)
Type: [address]b [label] / if no label, the default jump to the end of the script
# sed '{/first/b jump1;s/this is the/no jump on/
> :jump1
> s/this is the/jump here on/}' dataheader
no jump on header line
jump here on first data line
no jump on second data line

no jump on last line

2)Test command(test)
If the replace command successful match, test command will jump to the specified label
# sed '{
> s/first/matched/
> t //If the first replace command successful match, replaces, and skip the following command
> s/this is the/no match on / //If the first replace command does not match, the second orders will be executed
> }' dataheader
no match on header line
this is the matched data line
no match on second data line

no match on last line

# echo "this, is, a, test, to, remove, commas"|sed -n '{
> :start //Tags: begin to
> s/,//1p
> t start
> }'
this is, a, test, to, remove, commas
this is a, test, to, remove, commas
this is a test, to, remove, commas
this is a test to, remove, commas
this is a test to remove, commas
this is a test to remove commas

The 7 pattern substitution
1)The and symbol
# echo "the cat sleeps in the hat"|sed 's/.at/".at"/g'
the ".at" sleeps in the ".at"
# echo "the cat sleeps in the hat"|sed 's/.at/"&"/g' //&Symbols used to represent the substitution pattern matching in the command
the "cat" sleeps in the "hat"

2)Replace the single word
# echo "the furry cat is pretty"|sed 's/furry \(.at\)/\1/' //Use parentheses to define sub string substitution pattern, parentheses to escape
the cat is pretty //\1 represents the first module, represents second modules

8 in the script using sed
1)Use the packaging scripts
# vi reverse
#!/bin/bash

sed -n '{
1!G
h
$p
}' $1 //Use the first parameter
# ./reverse dataheader
this is the last line

this is the second data line
this is the first data line
this is the header line

Three the.Sed utility
1 remove HTML Tags

# cat index.html
<html><body><h1>welcome to hunter's computer!</h1></body></html>
# sed 's/<[^>]*>//g;/^$/d' index.html //s/<[^>]*>//G delete tags, /^$/d delete blank lines
welcome to hunter's computer!
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Ailsa at December 04, 2013 - 4:56 AM