shell(3)--sed

sed  
itself is a pipeline command that can replace, delete, add, and retrieve specific lines of data.

Basic usage:

sed [-nefr] [action]
option:
-n all data from STDIN will be output to the screen, after adding -n, only the line of data (action) processed by sed will be listed
-e directly in the command Editing sed actions in line mode
-f directly writes sed actions in a file, -f filename can execute sed actions in filename
-r sed actions support the syntax of extended regular notation. (The default is basic regular notation)
-i Directly modify the content of the read file, not output to the screen
Action: [n1[,n2]] function
n1,n2 Indicates the number of lines selected for action, which generally does not exist. If you want to perform 10,20 function
function on lines 10 to 20, you have the following options:
a new, followed by a string, and these strings will appear on a new line. (the current next line)
c is replaced, c is followed by a string, the lines between n1 and n2 are replaced by
d deleted, d is usually not followed by anything
i inserted, i followed by a string, inserted into the previous line of the current line .
p Print, print out a selected data. Usually used with the parameter sed -n to replace with
s to replace directly, such as vi replace command: 1,20s/old/new/g 
y conversion command
w write file

Prepare example file
$cat last > last.log 

Delete :
delete lines 2 to 5 of
last.log $cat last.log -n | sed '2,5d'
delete line 2
$cat last.log -n | sed '2d'
delete all lines after 2
$ cat last.log -n | sed '2,$d'

Add :
add 'aaabbb' after line 2
$cat last.log -n | sed '2a aaabbb'
If you want to add it before line 2, Then use the action of i
$cat last.log -n | sed '2i aaabbb'
to add multiple lines
$cat last.log -n | sed '2i aaabbb\ Continue to input after carriage return and line feed, and finally input ' (single quotation mark) and then Enter. For example:
[jamin@localhost ~]$ cat last.log -n |sed '2a aaa\
> bbb\
> ccc'

replace :
2 to 5 lines are replaced by: aaabbbb
$ cat -n last.log | sed '2,5c aaabbb'

extract (print) :
show only 2 to 5 lines
$ cat -n last.

(The result is only one, and the line number will be written above the line):
$ sed "=" last.log
61
reboot system boot 2.6.32-642.el6.x Thu Jan 12 06:47 - 08:13 ( 01:26) 

List Lines : The
list (list) command (l) can print text and non-printable ASCII characters in the data stream. Any non-printable character either precedes its octal value with a backslash, or uses standard C-style nomenclature. (The following command, only one line is taken)
$ sed "=" last.log
reboot system boot 2.6.32-642.el6.x Thu Jan 12 06:47 - 08:13 (01:\
26) $

line search and replace

sed 's/old/new/flags' file, which is the same as in vi.
flags option:
the number n that matches at the nth position will be replaced. Note: sed is line processing, so the line is calculated. For example: n=2 will replace the second matching
g, replace all
p, print the original content first,
w file, save the replacement result in the file (only save the line after the replacement)

Example
Take the ip of the current machine
$ ifconfig eth0 | sed -n '2p' | sed 's/^.*addr://'| sed 's/Bcast:.*//g'
analysis ifconfig eth0 | sed -n '2p' is the one that takes the ip one line followed by the perform replacement
remove cat /etc/man.config |grep 'MAN' commented line:
$ cat /etc/man.config |grep 'MAN'| sed '/^#.*$/d'
leaves only Comment the line
$ cat /etc/man.config |grep 'MAN'| sed '/^#.*$/ p' -n
leave only the comment line, grep can also
$ cat /etc/man.config |grep 'MAN ' | grep '^#'

The modified content of the above example is directly output to the screen. If you want to modify the original file directly, add the option -i. Such as:
$ sed '2,5c xxxxxx' -i last.log

Select row:

The above command uses numbers to select the rows to be processed, indicating the interval of the number of rows (counting from 1). The selection line in front of sed also supports regular expressions
Example:
View the line containing jamin, replace bash with csh
$sed '/jamin/s/bash/csh/' /etc/passwd
delete the root user's entry in the /etc/passwd file line
$sed '/root/d' /etc/passwd 

Use the -e option to execute multiple commands at a time to
separate the commands with a semicolon (you can also use the enter key to separate)
Example:
delete the second line, insert the test character before the 9th line
$cat -n last.log | sed '2d; The second way of writing 9a test'
(note that there is no need to add the escape character \ when newline)
$cat -n last.log | sed '2d
> 9a test' The
third way of writing:
$cat -n last.log | sed -e ' 2d' -e '9a test'
The fourth way to write: (use { }, delete lines 2 and 9, try not to add (a, i) and replace (c))
$cat -n last.log | sed '{2d
> 9d}'

Execute
the sed command from the file Write the sed command in the file, read the sed command from the file and execute it, use the -f parameter, which is suitable for a large number of sed commands to process files.
Example:
write sed command in script.sed file (delete second line, insert test on line 9):
2d
9a test
$sed -f script.sed last.log

Special character processing , to use \ escape character, you can also use the second way (the editor allows you to choose other characters as the string separator in the replacement command)
Example: replace /bin/bash with /bin/csh, The following three writings are the same
$sed 's/\/bin\/bash/\/bash\/csh/g' /etc/passwd
$sed 's!/bin/bash!/bash/csh!g' /etc /passwd
$sed 's-/bin/bash-/bash/csh-g' /etc/passwd

The conversion command
processes a single character, and cannot limit the position of the character.
Example: replace r in last.log with x 0 with y t with z. If the characters on both sides are not the same length, an error will be reported
#sed 'y/rot/xyz/' last.log

Write a file
Example: write lines 1 and 2 of the /etc/passwd file to the sed.log file
# sed '1,2w sed.log' /etc/passwd
write the line containing root to the sed.root file (-n do not display data to the screen)
# sed -n '/root/w root.sed' /etc/passwd 

Reading from a file The
r command allows inserting data from a file into the data stream
Example
Insert the last.log file data, after the first line in sed.log, and output it to the screen.
$sed '1r last.log' passwd.log 
Note:
$sed '1ar last.log' passwd.log #This operation is to insert r last.log into the
file after the first line of passwd.log as a string Finally, add the $ symbol directly.
# sed '$r num.sh' sed.log 

example:
use the data in a file to replace the point text in the file
root.sed The text is as follows:
this is test text
LIST
this is another test

replace LIST in the file with the line in /etc/passwd and delete LIST
$sed '/LIST/{ 
r /etc/passwd
d
}' root.sed 


Advanced:

The next command (single-line next command n)
Usually the sed editor will execute all defined commands on the current line before moving to the next text line in the data stream. The one-line next command changes the flow.
The n command will move the next line of text in the data stream to the workspace (pattern space) of the sed editor without having to go back to the beginning of the command and execute it again.
Example:
There is a file as follows:
this is first

this is second

this is third

Requirement: delete the line after the first line containing first:
$sed '/first/{n;d}' passwd.log 
Description: filter the line containing first , point the processed data stream to the next row (similar to a processing cursor), and execute the d command.

sed Multi-line processing
N Add the next line in the data stream to create a multi-group process (merge the next line into the previous line, which is the current processing line)
D Delete a line in the
multi-line group P Print the multi-line group Line

N: Merge the next line into the previous line, that is, the currently processed line
Example:
$last | sed '/jamin/{N;s/\n/ /}'
Explanation: Find the line containing jamin, and the N command puts the next line One line is merged into the current line, and the newline character (\n) is replaced by a space

. If you are looking for a phrase, which is divided into two lines, you can use the N command
Example:
test The content of the file is as follows (System Administrator has a newline):
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrators should attend.
----
On Tuesday, the Linux System Administrator's group meeting will be held.
On Tuesday, the Linux System
Administrator's group meeting will be held.
All System Administrator's should attend.
All System Administrator's should attend.

Replace System Administrator with Desktop User
$ sed 'N ; s/System Administrator/Desktop User/' test 1
$ sed 'N ; s/System.Administrator/Desktop User/' test 2
$ sed 'N ; s/System\nAdministrator/Desktop\nUser/;s/System Administrator/Desktop User/' test 3
$ sed 's/System Administrator/Desktop User/;N ; s/System\nAdministrator/Desktop\nUser/' test 4
$ sed 's/System Administrator/Desktop User/;N ;s/System Administrator/Desktop User/; s/System\nAdministrator/Desktop\nUser/' test 5
The first command, first put the next line of data and the currently processed Lines are merged, then replaced (note that only spaces are matched, so newlines are not replaced). But line 9 is also not replaced (will be explained later).
The second command adds a . (to match spaces and newlines) to the replaced original characters, so that the System Administrator of the newline on the first line is also matched. But the output combines the first and second lines (what the N command does); so there is a third command. (Note: the even-numbered newlines are not replaced.)
After the third command is combined, it matches the space character and the newline character (\n) respectively.
4th command: Execute the first 3 commands, you can see that the last line of the test file is not replaced, because: no other line can be read into the pattern space to merge with this line, so the N command will miss it. All in place the one-line command before the N command.
The 5th command: The problem with the 4th command: If the last line is an even line, it will be merged with the previous line, so it cannot match the characters separated by spaces, so it is necessary to add and replace the characters of spaces.
Question 1: The above example cannot replace the data that wraps on line 6. Such as the test file: Lines 6 and 7 have data wrapping. After processing the first row of data, the N command merges the 5th and 6th rows into the first row, and the 7th and 8th rows. So the above command can't handle even line wrapping data.
Solution:
Select the process from the second line, and then process it from the first line.
$sed '2,${s/System Administrator/Desktop User/;N ;s/System Administrator/Desktop User/; s/ System\nAdministrator/Desktop\nUser/}' test | sed '{s/System Administrator/Desktop User/;N ;s/System Administrator/Desktop User/; s/System\nAdministrator/Desktop\nUser/}'

delete multiple lines Command :
The preceding d command is a single-line delete, be careful when used with N, it may delete two lines of data.
Example:
still the test file when using the N command. The following command will delete the data combined into two lines.
$sed 'N;/System\nAdminist/d' test
D : Multi-line delete command, delete only the first line in the pattern space.
Example: delete the first line that matches (if the content to be deleted is in the first line, it can be used)
$sed 'N;/System\nAdminist/D' test

multi-line extraction (print)
P only print multi-line mode The first line in the space
Example: After matching, print the first line of content
$sed -n 'N;/System\nAdminist/P' test


Hold space (hold space) and pattern space (pattern)
Pattern space (pattern): is an active buffer, the default is an empty line. When the sed editor executes the command it saves the text to be checked.
Hold space: You can use hold space to temporarily save some lines while processing some lines in the pattern space.
The following commands are used to manipulate the hold space:
h copy the pattern space to the hold space (will replace the text in the hold space)
H append the pattern space to the hold space
g copy the hold space to the pattern space (replace the text in the other pattern space text)
G append hold space to pattern space
x swap contents of hold space and pattern space
These commands clear the pattern space to load other strings for processing.
Example:
The content of the test file is as follows:
This is the test line.
This is the first data line.
This is the second data line.
This is the last line.

$sed -n '/first/ {h ; p ; n ; p ; g ; p }' test
prints the result:
This is the first data line.
This is the second data line.
This is the first data line.
Analysis:
1. Match the first line
2, h command, copy the matched line to the hold space, then p command, print this line; so print out the first line
3, then n command, move the next line of data to the pattern space, and then Execute the p command, so the second line
4 is printed, followed by the g command, copy the hold space to the pattern space, and execute the p command, so the last line is printed

Exclude command!
Example:
test The contents of the file are as follows:
This is the test line.
This is the first data line.
This is the second data line.
This is the last line.

$sed -n '/test/!p' test
above If the command does not exclude the command ! , print the line that matches test. After adding !, the situation is reversed. When the test
line is matched, the p command is not executed, so all lines that do not match are printed out.
In "sed multiline processing", there is a problem with not replacing the last line of data, you can also use ! to achieve:
$sed 's/System Administrator/Desktop User/;N; s/System\nAdministrator/Desktop\nUser /' The processing method before test
$sed '{$!N;s/System Administrator/Desktop User/;s/System\nAdministrator/Desktop\nUser/}' test
description: $ indicates the last line, if it is the last line The N command is not executed.

Change flow (branch, test)
Usually sed will start at the top of the script and execute until the end of the script; the sed editor provides a way to change the flow of command script execution; the result is similar to structured programming.
Change flow -- branch The
syntax is as follows: [address]b [label]
The address parameter determines which lines of data will trigger the branch command, label is the position to jump to the command; if there is no label, it will jump to the end of the command line.
Example 1:
The content of the test file is as follows:
This is the test line.
This is the first data line.
This is the second data line.
This is the last line.
Replace This is with Is this, passing the second and third lines , line. Replace with test? 
$sed '{2,3b;s/This is/Is this/;s/line./test?/}' test
description: In branch commands, the second and third lines are skipped followed by the replacement command. So only the first and last lines are replaced.
Example 2:
Example 1 jumps straight to the end of the command line. You can use label to define the label to jump to. Labels start with a colon and last 7 characters.
$ sed '/first/b jump;s/This is/No jump/; :jump s/This is/Jump to/' test
description: when the line matches first, jump to the :jump position to execute the command; if not If they match, they are executed sequentially. Results of the:
No jump the test line.
Jump to the first data line. #match to the first line
No jump the second data line.
No jump the last line.

Example 3:
$ sed '/first/b jump;s/This is/No jump/; :jump s/This is/Jump to/;s/line./test.../' test
description: On the basis of the above, replace line. as well. But this replacement is not inside the :jump tag. When the line does not match first, it will be executed sequentially, but the command after :jump will not be executed. So, on the basis of example 2, replace all line. with test...
Question: How to write multiple commands after :label?

Example 4:
The above examples all jump to the label after the command, or the label can be defined to the front, so that the effect of the loop is achieved.
$ echo 'this, is, a, test, to, remove, commas.' | sed -n '{:start s/,//1p;b 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 above command, only ctrl+c will end, the command to deal with this problem is as follows, only if it matches ',' will it jump to the start label
$ echo 'this, is, a, test, to, remove, commas.' | sed -n '{:start s/,//1p;/,/b start}'

change the flow -- test
test command t, similar to branch command b, but b jumps to the branch according to the address, and the test command t jumps to a label according to the result of the replacement command.
Syntax:
[address]t [label]
If the replace command successfully matches and replaces a pattern, the test command jumps to the specified label, or to the end of the script if no label is specified. If it fails to match the specified pattern, it will not jump and execute the script sequentially (do not execute the commands in the label).
Example:
The content of the test file is as follows:
This is the test line.
This is the first data line.
This is the second data line.
This is the last line.

$ sed '{s/fist/match/
> t;s/This is the/Not match/;
> }' test
Note: t cannot be written with the replacement command.

Example: Implementing Loop Replacement Using the t Command
$ echo 'this, is, a, test, to, remove, commas.' | sed -n '{:start s/,//1p;
> s/,//1p
> t start}' 

The pattern replaces
the ampersand, which refers to the matched character.

Example:
$ echo "The cat sleeps in his hat." | sed 's/.at/"&"/g'
Explanation: & is the string matched by the previous .at, whatever is matched, & is what.
Substitute individual words
& signs Extract the entire string matching the pattern specified in the replace command.
Parentheses () can extract part of this string.
Example:
$ echo "The System Administrator manual" | sed 's/\(System\) Administrator/\1 user/'
The System user manual
Instructions:
1. Use () for subpatterns and escape
2. Substitute characters The \1 in the string refers to the string of the first subpattern wrapped by (), which is System in this case. By analogy \2, \3 are the second and third sub-patterns respectively.
3. Subpatterns can be referenced by special characters, then the replacement character is the matched string.
Usage scenario: You need to replace a phrase with a word, and the word is part of the phrase, which is particularly convenient at this time. For example:
$ echo "That furry cat is pretty" | sed 's/furry \(.at\)/\1/'
That cat is pretty
can't use &, & will replace the entire matched pattern. When inserting text between two or more subpatterns.
Example: (the parentheses are replaced by Chinese, otherwise they will not be displayed)

cho echo '1234567' | sed '{: start
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
t start
}'

The result is: 1,234,567
Description: There are two matching sub-patterns above. *[0-9] and [0-9]{3} The result of the first match is \1 is 1234, \2 is 567, and so on .

Using sed in a script
1. Write the sed command in a shell script. You can use ordinary shell variables with the sed editor.
Example:
The sed.sh script is as follows:
#!/bin/bash
sed '/first/b jump;s/This is/No jump/; :jump s/This is/Jump to/' $1
uses (test is the example above file):
$./sed.sh test


2. Redirect the output of sed The
sed editor will output the result to STDOUT by default. You can use $() to redirect the output of the sed editor to a variable, and use it in the following script
Example:
The sed.sh script is as follows: (Use  view plain to view. Where \( can't be shown )


#!/bin/bash
num=$1
start=1
involution=1
for((;start<=num;start++))
do
    involution=$(($involution * $start))
done

result=$(echo $involution |sed '{
:start
s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/
t start
}')
echo $result

Description: First calculate the factorial of the incoming parameter, save it in the involution variable, and then insert a comma into the resulting 3-digit number. The sed editor result is stored in the result variable
such as :
$./sed.sh 20
2,432,902,008,176,640,000

sed utility
1. Add a single line spacing (add a blank line to the first line).
$ sed '$!G' test
description:
a- use the above test file
b- do not execute G on the last line
c- hold space The default value is a blank line, the G command appends the hold space to the pattern space
if the test file has If there are multiple blank lines, the above command cannot be used. First delete all blank lines in the data stream
, then use the G command to insert new blank lines after all lines.
$ sed '{/^$/d;$!G}' test

2. Number the file (similar to cat -n)
$ sed '=' test | sed '{N;s/\n/ /g}'
Result (the first part is to add the line number, the latter part is to combine the line number and content into one line, separated by spaces. Only one line of results is taken)
1 This is the test line.

3. The key to deleting consecutive blank lines
is to find the blank Lines and address ranges that are not blank lines, and the lines outside this range can be deleted.
$sed '/./,/^$/!d' test
description: The range is /./ to /^$/. The starting address of the range will match any line containing at least one character. The end address of the range will match a blank line. Rows within this interval will not be deleted.

4. Delete the switch blank line of the file
$sed '/./,$!d' test
Description: This interval starts from the line containing characters until the end of the data stream. Any lines within this interval will not be removed from the output.

5. Delete trailing blank lines
$ sed '{:start /^\n*$/{$d;N; b start}}' test
Description: There is a curly brace inside the curly braces of the above command, which is a command group. The command group will be applied to the specified address mode. Address patterns can match lines that contain only one newline character. If such a line is found, and it is the last line, the delete command deletes it. If it's not the last line, the N command appends the next line to it, and the branch command jumps to the beginning of the loop and starts over.


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326748765&siteId=291194637