Sed common usage Summary

Source: Internet
Author: User
Merge is added after the matched row

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 the data in the first line with xyzsed/ ^ bb/I \ kjdlfkjdslkf temp.txt // Add a new sed row before the matched row/^ bb/a \ kjdlfkjdslkf temp.txt // Add a new row after the matched row

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 replace all, replace sed 's.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. a Ppend/= '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.
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.