Linux sed command

Source: Internet
Author: User

----------------------------------
Sed-n '117,117 P' abc.txt | more

$ Sed '2d 'example ----- Delete the second line of the example file.
$ Sed '2, $ d' example ----- delete all rows from the second row to the end of the example file.
$ Sed '$ d' example ----- Delete the last row of the example file.
$ Sed '/test/'d example ----- delete all rows containing test in the example file.

Replace: s command

$ Sed's/test/mytest/G' example ----- replace test with mytest in the entire row. If no g tag exists, only the first matched test in each row is replaced with mytest.

$ Sed-n's/^ test/mytest/p 'example ----- (-n) option and the p Flag are used together to print only the replaced rows. That is to say, if the test at the beginning of a row is replaced with mytest, print it.

$ Sed's/^ 192.168.0.1/& localhost/'example ----- & symbol indicates replacing the part found in the string. All rows starting with 192.168.0.1 are replaced with their own localhost and changed to 192.168.0.1localhost.

$ Sed-n's/\ (love \) able/\ 1rs/p 'example ----- love is marked as 1, and all loveable will be replaced with lovers, the replaced line is printed out.

$ Sed's #10 #100 # g'example ----- whatever the character, followed by the s command is considered as a new separator. Therefore, "#" is a separator here, replaces the default "/" separator. Replace all 10 with 100.

Range of selected rows: comma

$ Sed-n'/test/,/check/P' example ----- all rows within the range specified by the template test and check are printed.

$ Sed-n' 5,/^ test/P' example ----- print all rows starting from the fifth line to the first line that contains the beginning of test.

$ Sed '/test/,/check/s/$/sed test/' example ----- for the rows between the template test and west, the end of each row is replaced by the string sed test.

Multi-Point Editing: e command

$ Sed-e '1, 5d '-e's/test/check/'example ----- (-e) options allow multiple commands to be executed in the same line. As shown in the example, the First Command deletes lines 1 to 5, and the second command replaces test with check. The command execution order has an impact on the result. If both commands are replacement commands, the first replacement command will affect the result of the second replacement command.

$ Sed -- expression ='s/test/check/'-- expression ='/love/d' example ----- a better command than-e is -- expression. It can assign values to sed expressions.

Read from file: r command

$ Sed '/test/r file' example ----- the content in file is read and displayed after the row matching test. If multiple rows match, the file content is displayed under all matched rows.

Write File: w command

$ Sed-n'/test/w file' example ----- all rows containing test in example are written to file.

APPEND Command: command

$ Sed '/^ test/a \ ---> this is a example 'example <----- 'this is a example' is appended to the end of the row starting with test, sed requires that command a be followed by a backslash.

Insert: I command

$ Sed '/test/I \\

New line

------------------------- 'Example

If test is matched, the text following the backslash is inserted before the matching row.

Next: n command

$ Sed '/test/{n; s/aa/bb/;}' example ----- if test is matched, move it to the next row of the matched row and replace aa of this row, change to bb, print the row, and continue.

Deformation: y command

$ Sed '1, 10y/abcde/ABCDE/'example ----- converts all abcde in line 1-10 to uppercase. Note that this command is not applicable to the regular expression metacharacters.

Exit: q command

$ Sed '10q' example ----- exit sed after printing the 10th rows.

Maintain and obtain: h and G commands

$ Sed-e '/test/H'-e' $ G example ----- when sed processes files, each row is saved in a temporary buffer called a mode space, all processed rows are printed on the screen unless the row is deleted or the output is canceled. The mode space is cleared, and a new row is saved for processing. In this example, the row Matching test is found and saved to the mode space. The h Command copies the row and saves it to a special buffer zone called the guaranteed cache. The second statement means that when the last line is reached, the G command extracts the row that maintains the buffer and places it back in the mode space, and append it to the end of the row that already exists in the mode space. In this example, It is appended to the last row. Simply put, any row containing test is copied and appended to the end of the file.

Persistence and interchange: h and x commands

$ Sed-e '/test/H'-e'/check/X' example ----- swap mode space and keep the buffer content. That is, to swap the rows containing test and check.

7. Script

Sed script is a list of sed commands. When Sed is started, the file name of the script is guided by the-f option. Sed is very picky about the commands entered in the script. There cannot be any blank or text at the end of the command. If there are multiple commands in one line, separate them with semicolons. Comments rows starting with # And cannot span rows.

-----------------------------------

 

Use the sed s command to replace large text that contains single quotes, greater than signs, and equal signs
= 'Change to = /'
'> Change to/'>
/N to/n
/R to/r
Discover/escape characters during use
Sed-I. bak's // n/G' bigtext.txt
Sed-I's /// r //// r/G' bigtext.txt
Sed-I's/= ''/= //''/G' bigtext.txt
Can be replaced correctly
'> I don't know how to replace it with/'>

# Cat abc
# ='
# '>
# \ N
# \ R
# [Root @ mail ~] # Sed "s/'>/\'>/" abc
# ='
# \ '>
# \ N
# \ R

Sed's/111111111111/222222222222/'abc.txt does not modify the file, only print it out
Sed-I's/222222222222/333333333333/'abc.txt Modify file

Sed "s/'200'/3333333333/" abc.txt
Sed-I "s/'20140901'/3333333333/" abc.txt

Sed-I "s/'2017-000000', 0574, 86839005 '/'2017-000000', 13070, 29.91/" abc.txt

Sed-I "s/'2017-000000', 0574, 86839005 '/'2017-000000', 13070, 29.91/" biz_infos_bak. SQL



Author: tongxiaoming520

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.