Linux sed command detailed

Source: Internet
Author: User

Sed is a kind of stream codec, which is a very important tool in text processing, and it can be used perfectly with regular expression, so the work can be remarkable.

When processing, the currently processed row is stored in a temporary buffer, called the "Pattern Space" (oattern spaces), then the SED command to process the contents of the buffer, after processing, the contents of the buffer is sent to the screen display.

Then the next line is repeated until the end of the file. The contents of the file are not changed unless the Write command is used to update the content.

Addressing is used to decide which rows to edit. The address can be in the form of a number, a regular expression, or a combination of both. If no address is specified, SED processes all lines of the input file.

sed command function
The command function a\ adds one or more rows after the current line. Multiple rows except the last line, the end of each line requires "\" To continue c\ the text in the current line with the new text after this symbol. When multiple rows are not the last line, the "\" continuation is required at the end of each line i\ to insert text before the current line. Multiple rows In addition to the last row, each line at the end of the need to "\" continue to delete the row h to copy the contents of the schema space to the staging buffer h the contents of the schema space is appended to the staging buffer g the contents of the staging buffer to the schema space, overwriting the original content G to the contents of the staging buffer into the pattern space Append to the original content after L list nonprinting characters p print line n read into the next input line and start processing it from the next command instead of the first command Q to end or exit sed r to read the input line from the file! Apply command s to all rows other than the selected row replaces another G with one string replace W in row to write the selected row to the file X swap staging buffer with the contents of the pattern space Y replaces the character with another character (cannot use the Y command for regular expressions)
SED common Command parameters
P==print  D:delete =: Prints the line number of the matching line-n cancels the default full output as long as required-   e allows multiple edits-I to modify the file contents-R does not need to be escaped
Common Command Display

SED specializes in line output, with source files:

HHH PTS/1 192.168.25.1 Sat June 22:04 still logged in reboot system boot 2.6.32-358.EL6.I sat June 22:04-22:43 (00:38) OMC pts/0 192.168.25.1 Sat June 20:16-down (00:00) reboot system B   Oot 2.6.32-358.EL6.I Sat June 19:38-20:16 (00:37) root pts/1 192.168.25.1 Sat June 12:20-down (00:55) Root pts/0 192.168.25.1 Sat June 11:53-down (01:22) reboot system boot 2.6.32-358. EL6.I Sat June 11:52-13:15 (01:23) root pts/0 192.168.25.1 Sat June 05:40-down (02:51) Reb  oot system boot 2.6.32-358.el6.i Sat June 05:38-08:32 (02:54) root pts/0 192.168.25.1 Fri June 29 21:01-down (06:21) wtmp begins Tue June 9 03:57:56 

Print only the third line

Sed-n ' 3p '/var/log/yum.log

View only lines 3rd through 9th of a file

Sed-n ' 3,9p '/var/log/yum.log

Filter a specific string to display the positive line content

sed-n  '/root/p ' Yum.log

Show rows that contain "HHH" lines to rows that contain "OMC"

Print 1-5 lines and display line numbers

Sed-n-E ' 1,5p '-e ' = '   Yum.log

Displays only the line number of the matched string

Print the contents and symbols of the matching lines "as if they were followed by a keyword query once"

Sed-n-E '/root/p '-e '/root/= ' Yum.log

Replace the root "real write" in the Yum.log file with world

Sed-i ' s/root/world/g ' yum.log==>sed-i ' s#hhh#hhhh#g ' h.txt     with S==search  Find and replace              G==global  Replace All              -i:implace

Print the last line

Sed-n ' $p ' Yum.log

Add happy to the first line of the file, add New Year at the end of the file

Sed-e ' 1i Happy '-e ' $a New Year ' Yum.log '       interface shows "Sed-i-e ' 1i Happy '-e ' $a New Year ' Yum.log    " real Write file "

Add hahaha below each line in the first and fourth lines of the file

Sed ' 1,4i hahaha ' yum.log       

The & symbol represents the result of the last match in the SED command

Sed ' s/world/hello_&/g ' yum.log   

Delete lines 3rd through 9th, just not shown.

Sed  ' 3,9d '/var/log/yum.log

Delete rows that contain "HHH" to lines between rows that contain "OMC"

Sed '/hhh/,/omc/d ' yum.log

Delete the line to line tenth containing "OMC"

Sed '/omc/,10d ' yum.log


Note: Ranges can be represented by numbers, regular expressions, or combinations of both

Display 5 to 10 lines in the match root, replace all the root in the line with the FTL, and print to the screen

Sed ' 5,10 s/root/ftl/g ' Yum.log "only show with" sed-i sed ' 5,10 s/root/ftl/g ' yum.log '-I will actually replace information "

-E is an edit command for multiple editing tasks

After removing 1 to 5 lines, replace reboot with Shutdownsed-e ' 1,5d '-e ' = '-e ' s/reboot/shutdown/g '  yum.log  

Print 3 to 6 lines of content and match reboot to shutdown

Sed-n-E ' 3,6p '-e ' s/reboot/shutdown/g '  yum.log  

the-r command is a read command. SED uses this command to add the contents of a text file to a specific location in the current file

[email protected] log]# cat test.txt [[email protected] log]# sed '/root/r/var/log/test.txt ' yum.log '  yum.log  After the root is read into the Text.txt content under the match content, only displays the "

-W is a write command

Sed  '/root/w/home/omc/root.txt ' yum.log '       yum.log match root Write Root.txt ', true write ' sed-n '/root/w/home/omc/root.txt ' Yum.log '    after adding the-n parameter, the source file is not printed "ll/home/omc/root.txt


Print 1-5 lines and display line numbers

Sed-n-E ' 1,5p '-e ' = '  

Regular matching of SED

Regular matching IP and subnet masks

[Email protected] log]#  ifconfig |sed-n ' 2p '      [[email protected] log]# ifconfig |sed-n ' 2p ' | sed-r ' S#.*r: (. *) B.*k: (. *) #\1 \2#g '       (. *) indicates a matching item, after which you can remove the matching contents of the first parenthesis with \1, \2 take out the matching contents in the second parenthesis  

Use with the Find command

Find. -name  "*.txt" |xargs   sed-i ' s/hhhh/\hhhh/g ' find.-name  "*.txt" |xargs   sed-i ' s#hhhh#hhhh#g ' find.-na Me  "*.txt"-exec sed-i ' s/hhhh/\hhhh/g ' {} \;find.-name  "*.txt" |xargs Cat

SED error usage

Sed ' s#,# #h ' h.txt | Cut-d ""-F 3,5 h.txt    <--error usage, previously processed, followed by cut view h.txt file sed ' s#,# #h ' h.txt | Cut-d ""-F 3,5          -right usage

Linux sed command detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.