Linux sed command parameter usage detailed

Source: Internet
Author: User
Tags control characters html tags first row

Use script to work with text files.

Syntax: sed [-hnv][-e<script>][-f<script file >][text file]

Additional Note: SED can process and edit text files according to script instructions.

Parameters

-e<script> or--expression=<script> the input text file with the script specified in the option.

-f<script file > or--file=<script file > process the input text file with the script file specified in the option.

-h or--help display Help.

-N or--quiet or--silent displays only the results of the script.

-V or--version display version information.

Basic sed Edit command:
SED edit command
P Print Matching rows
= Show file line number
A to append the new text information after the line number is positioned
I inserts new text information after the line number is positioned
D Delete the Anchor line
C replaces positioned text with new text
s replaces the corresponding pattern with the replacement pattern
R reading text from another file
W write text to a file
Q The first pattern match is completed after launch or immediately launched
L Show control characters equivalent to octal a S C i code
{} A command group executed at the location line
N reads the next line of text from another file and attaches it to the next line
G Paste Mode 2 to/pattern n/
Y transfer character
N continues to the next input line; pattern matching statements that allow cross rows


Common Command Parameters

Sed ' 1,3d ' file//delete 1-3 lines

Sed '/[aa]bc/p ' file//print line that contains ABC or ABC

Sed '/abc/d '//delete line containing ABC

Sed '/abc/!d '//Do not delete line containing ABC

Sed ' 5, $d '/delete all from line fifth to last line

SED 1q//Show First row

Sed ' s/$/.../g '//The last side of each row added ...

Sed ' s/d. (SK)/1&/g '//Meet D*sk Word, add sk sed ' s/<[^>]*>//g '/remove HTML tags before word

Sed '/abc/a123 '//Add a row with a row value of 123 after the line containing ABC

Sed '/abc/i123 '//Unlike A is I is inserting the front of the current line

Sed '/abc/c123 '//The line containing ABC will be changed to 123

Sed '/abc/{n;s/123/abc/g} '//Exact match contains ABC's next line, replace 123 all to ABC

Sed '/abc/{s/a/a/} '//replacement of a with ABC with a

Sed-e '/abc/h '-e ' $g '//will copy the line containing ABC, added on the last line.

Sed-e '/abc/{h;d} '-e '/123/g '//will copy and delete rows that contain ABC, added after the line containing 123. G is equal to the current row, G is the new row after the qualifying row. Sed ' s/^//g '//Add a space before each line

Sed ' s/. (.) $/*1/'//replace the penultimate digit with *

Sed '/abc/!s/1/*/g '//in rows that do not contain ABC, replace all 1 with *

Sed ' s/1|2|3/*/g '//in GNU will replace either 1 or 2 or 3 to *

Sed-i '/root/s/no/yes/'/etc/ssh/sshd_config sed in this text a row of root, matching the root row, replace No to Yes.

Replace the row containing now with the timestamp and the number of 8080 variable requests

Sed-i "/now/cnow$time_stamp 8080 request= $web 1tomcat"/var/www/html/munin/web1-2.html

Inserts a row at the position of a line

If you just append a line to a document, it's easy. >> can be done. However, if you insert it in a specified row.

Sed-i "8 S/^/alias vi= ' vim '/"/ROOT/.BASHRC

is to insert the alias vi= ' vim ' on line 8th.

Insert text before the first line

Sed-i ' 1 I insert string ' filename

Insert on last line

Sed-i ' $ A insert string ' filename

Inserts before a matching row

Sed-i '/pattern/i insert string ' filename

Inserts after a matching row

Sed-i '/pattern/a insert string ' filename

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.