Use sed to operate files

Source: Internet
Author: User
Use sed to operate the file [plain] I. the additional text www.2cto.com uses a \ to append one or more rows to the end of the specified row. if the placement position is not specified, it is placed to the end of each row by default. When appending text, you cannot specify a range. only one address mode is allowed. Additional format :...
Use sed to operate the file [plain] I. the additional text www.2cto.com uses a \ to append one or more rows to the end of the specified row. if the placement position is not specified, it is placed to the end of each row by default. When appending text, you cannot specify a range. only one address mode is allowed. Additional format: [address] a \ text \... text note: 1. a \ notifies sed to append the content after. 2. each row is followed by "\". when sed is executed, a new row is created and the content is added. 3. the last line cannot contain "\". Example: If you remove "\" at the end of the first line of the append, an error will be reported when running the script. Pg append. sed #! Bin/sed-f # append text/company/a \ Then suddenly it happened. _ yeeXun. after Execution: sed-f append. sed quote.txt sed: Unrecognized command: _ yeeXun. pg append after modification. sed #! Bin/sed-f # append text/company/a \ Then suddenly it happened. \ _ yeeXun. execute the script: sed-f append. sed quote.txt The honeysuckle band played all night long for noly $90. it was an evening of splendid music and company. then suddenly it happened. _ yeeXun. too bad the disco floor fell through at 23:10. the local nurse Miss P. neave was in attendance. because sed does not deal with the original file, it only edits a copy of it, so the data we append will not be written to the file; after executing the above command, I Quote.txt File: pg quote.txt The honeysuckle band played all night long for noly $90. it was an evening of splendid music and company. too bad the disco floor fell through at 23:10. the local nurse Miss P. neave was in attendance. II. modify the text using c \ to modify the specified row. format: [address [, address] c \ text \... text address: specifies the row position. you can use a regular expression to locate the row or directly specify the row number. Text: the replaced text. Note that the last line does not contain "\". The example replaces the third row of data: pg change. sed # change date/bad/c \ [Two good.] \ next goods. run the script sed-f change. sed quote.txt The honeysuckle band played all night long for noly $90. it was an evening of splendid music and company. [Two good.] next goods. the local nurse Miss P. neave was in attendance. www.2cto.com 3. add text to add data before a specified row. [Address] I \ text \... text address: specifies the row position. you can use a regular expression to locate the row or directly specify the row number. Text: the replaced text. Note that the last line does not contain "\". Example: pg insert. sed # insert data /. * ing/I \ [the insert date]. run the following script: sed-f insert. sed quote.txt The honeysuckle band played all night long for noly $90. [the insert date]. it was an evening of splendid music and company. too bad the disco floor fell through at 23:10. the local nurse Miss P. neave was in attendance. 4. delete the content of a text file, delete one or more consecutive rows, or match all rows in a certain mode in the format of [address [, address] d file information: pg quote.txt The honeysuckle band played all night long for noly $90. it was an evening of splendid music and company. too bad the disco floor fell through at 23:10. the local nurse Miss P. neave was in attendance. delete the first line: sed '1D 'quote.txt It was an evening of splendid music and company. too bad the disco floor fell through at 23:10. the local nurse Miss P. neave was in attendance. delete the first to third rows: sed '1, 3d 'quote. t Xt The local nurse Miss P. neave was in attendance. delete The last row: sed '$ d' quote.txt The honeysuckle band played all night long for noly $90. it was an evening of splendid music and company. too bad the disco floor fell through at 23:10. use a regular expression to delete all rows that meet the conditions. Delete the rows that start with a, end with d, and have a total length of 3. the rows that match the first line (band) and the second line (and ). Sed '/. d/d 'quote.txt Too bad the disco floor fell through. the local nurse Miss P. neave was in attendance. 5. replace text in the format of [address [, address] s/pattern-to-find/replacement-pattern/[g p w n] address: Option g: by default, replace only the first appearance mode, and use g to replace all appearance modes. p: by default, sed writes all replaced rows to the standard output. adding p will invalidate the-n option; -n does not print output results. W: file name. use this option to direct the output to a file. Www.2cto.com replace The lower-case floor with The upper-case FLOOR: sed's/floor/FLOOR/'quote.txt The honeysuckle band played all night long for noly $90. it was an evening of splendid music and company. too bad the disco FLOOR fell through at 23:10. the local nurse Miss P. neave was in attendance. delete the line $ symbol that contains the "$" symbol: the line containing the $ symbol: sed-n'/$. */p' quote.txt The honeysuckle band played all night long for noly $90. delete The row with The symbol "$ (replaced by null)": sed-n's/\ $ // P' quote.txt The honeysuckle band played all night long for noly 90. replace The first "THE" word with "The" and output it to another file if sed. if out exists, write data to it. if not, create this file and write data: sed's/The/THE/w sed. out 'quote.txt THE honeysuckle band played all night long for noly $90. it was an evening of splendid music and company. too bad the disco floor fell through at 23:10. THE local nurse Miss P. neave was in attendance.
View THE file sed. out: pg sed. out THE honeysuckle band played all night long for noly $90. the local nurse Miss P. Neave was in attendance. -- THE end --
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.