Sed common usage summary, sed Summary

Source: Internet
Author: User

Sed common usage summary, sed Summary

Edit the text sed-I '1i xyz' test.txt before the first line sed-I '1a xyz' test.txt insert sed-I '1c xyz' test.txt after the first line replace row data with xyzsed '/^ bb/I \ kjdlfkjdslkf' temp.txt // Add a new row of sed '/^ bb/a \ kjdlfkjdslkf' temp.txt // in the match add the new line sed-n '/xxx/w temp.txt' temp1.txt // temp1.txtto the line with xxxinserted to temp.txt sed '/xx/c \ sdfdsf' temp.txt // replace matching xx with a new row of data line sed '1d 't.txt/Delete the first line of the file sed'/bro/d' t.txt // Delete the line with bro (actual modification, delete-I) sed '/^ $/d' t.txt // Delete Delete empty rows sed's/^. * uid // 't.txt // Replace the character before the uid with null. ^ the first character cannot be ., in addition, the character and * connections in sed must be added. sed's/night/NIGHT/'1.txt // replace night (add-I to directly modify the source file 1.TXT, instead of outputting the replaced data to the screen) sed's/night/NIGHT/G' 1.TXT is replaced with all, and only sed's/is replaced once without adding a g line /. html // 'B .txt> b0.txtsed 'y/bo/BO/'test.txt // replace B with B, o replace it with Osed 'y/bb ll/bb ll/'test.txt // replace bb with BB, replace ll with LL to query the text sed '/he/w test1.txt' test.txt // read test.txt's content matching line and write it to test1.txt sed '1, 2 w test1.txt 'test.txt // Read test.txtcontent, and write test1.txt sed '/he/r test1.txt' test.txt // read test.txtcontent matching all the contents of test1.txt and output sed-n'1, 3 p 'getrow. sh // display the meat of one row to three rows sed-n'1,/hello/'P orig.txt // print from the first line, print to the first row containing hello // n to cancel the default output and print only the rows containing the template. By default, all rows are printed (edited and not edited) sed-n' $ P' getrow. sh // sed-n'/echo/'P while. sh // print the line that contains the echo characters/pattern/mode sed-e '/echo/=' while. sh // and print the row number (and the entire file is printed)-n only print the actual row number print the row number using = sed-n-e '/dfs. support. Append/= 'hdfs-default.xmlsed-n-E'/we/p'-E'/we/= '2.txt // print only matching rows, parallel Display row number sed-n'/aa/= 'aa.txt // only display the matched row number sed-n'/^ h/'P test.txt // display the row starting with h sed '/^ hello/d' test.txt // query the format of sed-n'/s \ {2 not starting with XX, \}/'P test.txt/s character matches sed-n'/[0-9] \ {1, \}/'P test.txt // The row that contains numbers. [0-9] + does not support sed '2q' test.txt // print it to the second row and exit. Note that, sed does not directly operate on the initial data. It operates on a copy of the original data. When sed is processing, the current row is stored in the temporary buffer, and then the content in the buffer is processed. After processing is complete, if the content in the buffer is not redirected to a file, the content in the buffer is sent to the screen, process the next row until the processing is complete.



Sed usage

1. sed's/#. * $ // G' deletes #. * from the row ending #. *.
2, sed '/^ $/d to delete empty rows. In the regular expression, ^ indicates the beginning of the line, and $ indicates the end of the line. ^ $ Indicates empty rows. D Indicates deletion.
3. sed's/[0-9.] \ +/abc/'IP \ + indicates the number of matches and indicates matching as many times as possible. Here we replace "192. 168." with "abc" for Multiple matching times.

Linux shell programming sed usage

1. s /. * (time. *) MS/\ 1/, I think you do not know the main is \ 1 This, I give an example, s/regexp/\ 1 /, this statement replaces the content that matches the expression regexp with the content of the 1st sub-expression in regexp. In your example, s /. * (time. *) MS/\ 1/, 1st regular (time. *) is a subexpression. The \ 1 in the second expression is a sub-expression that will satisfy the requirements. * (time. *) Replace the content of ms with the 1st subexpression (time. *), so the output is time = XX

2. s /. * time = //. To understand how to replace text in sed, s/regexp1/replacement/replaces the content matched by regexp1 expressions with replacement, in your example, the expression to be replaced is /. * time =/, the replaced content is null, //, so the actual meaning is /. * time =/matched content deleted, output as xx MS

3. [0-9.] indicates matching numbers 0-9 or.. + indicates repeating the previous match for more than one time. The whole meaning is matching consecutive numbers or periods.

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.