sed command in Linux

Source: Internet
Author: User
Tags first string

Sed

Sed means the stream editor, which is commonly used as a filter in shell scripts and makefile, which introduces the output of the previous program to the input of SED, which is converted to another format output through a series of editing commands. Sed and vi both originate from the early Unix Ed tools, so many SED commands are the same as the last command of VI.

The basic format of the SED command line is

SED option ' script ' file1 file2 ... sed option-f scriptfile file1 file2 ...

Option meaning:

--version            Displays the SED version. --help               Displays the help document. -N,--quiet,--silent  silent output, by default, the SED program automatically prints the contents of the pattern space after all script instructions have been executed, and these options can mask automatic printing. The-e script            allows multiple script directives to be executed. -F Script-file,--file=script-file   reading script instructions from a file is great for writing an automated scripting program! -I,--in-place        directly modify the source file, after the script instructions processed content will be output to the source file (the source file is modified) use caution! -L N,--line-length=n This option specifies the length of the governor that the L instruction can output, and the l instruction is used to output nonprinting characters. --posix             disables the GNU SED extension feature. -R,--regexp-extended  use extended Regular expression-s in script directives,--separate      by default, SED takes multiple filenames specified by the command line as a long contiguous input stream. The GNU SED allows them to be treated as separate files, so that regular expressions do not cross-file matches. -U,--unbuffered    the minimum cache input and output.

The above is only the option function description of the SED program itself, as for the specific script instructions (that is, the operation of the contents of the file) we will describe in detail, here is a brief introduction of several script instructions as an example of the SED program.

A,append        Append i,insert        insert d,delete        Delete s,substitution  replace

such as: $ SED "2a itcast"./testfile add "Itcast" after the second line of the output testfile content.

$ sed "2,5d" testfile

The files processed by SED can be either redirected from a standard input, or when command-line arguments are passed in, command-line arguments can pass in multiple files at once, and SED is processed sequentially. The edit command for SED can be passed directly to the command line parameter, or it can be written as a script file and then specified with the-f parameter, the format of the edit command is

/pattern/action

Where pattern is a regular expression, action is an edit operation. The SED program reads the pending file one line at a time, and if a line matches the pattern, the action is executed, and if a command has no pattern and only action, the action will be used for each row of the file to be processed.

Commonly used SED commands
/pattern/p  prints lines that match the pattern/pattern/d Delete rows matching the pattern  /pattern/s/pattern1/pattern2/   find rows that match the pattern, Replace the first string that matches pattern1 in the row with pattern2/pattern/s/pattern1/pattern2/g to  find the line that matches the pattern, and replace all strings that match pattern1 in the row with pattern2

Using the P command, it is important to note that SED outputs the contents of the pending file along with the processing results to the standard output, so the P command indicates that the line that matches the pattern is printed in addition to the contents of the file. For example, a file testfile content is

123abc456

Print lines that contain ABC

$ sed '/abc/p ' testfile123abcabc456

To output only the processing results, you should add the-n option, which is equivalent to the grep command

$ Sed-n '/abc/p ' testfileabc

Using the D command does not require the-n parameter, such as deleting a line containing ABC

$ sed '/abc/d ' testfile123456

Note that the SED command does not modify the original file, and the Delete command only indicates that some rows do not print output, rather than deleting from the original file.

When using the Find and Replace command, you can copy the string that matches pattern1 to the pattern2, for example:

$ sed ' s/bc/-&-/' testfile123a-bc-456pattern2 in & represents the string in the current line of the original file that matches the PATTERN1

Another example:

$ sed ' s/\ ([0-9]\) \ ([0-9]\)/-\1-~\2~/' testfile-1-~2~3abc-4-~5~6

The \1 in pattern2 represents the content that matches the first () parenthesis of pattern1, and \2 represents the content that matches the second () parenthesis of pattern1. SED uses the basic regular expression specification by default, and if the-r option is specified with the extended specification, then the () parentheses do not have to be escaped.

$ sed  ' s/yes/no/;s/static/dhcp/'  ./testfile Note: Use semicolons to separate instructions. $ Sed-e ' s/yes/no/'-e ' s/static/dhcp/' testfile Note: Use the-e option.

If the content of Testfile is

Now remove all HTML tags so that the output is

Hello Worldwelcome to the world of regexp!

How do you do it? If you use the following command

$ sed ' s/<.*>//g ' testfile

The result is two blank lines that filter out all the characters. This is because the quantifier in the regular expression matches the string as long as possible, which is called greedy (greedy). For example, when the SED is dealing with the first line,,<.*> matches not or such tags, but

Such a whole line, because this line starts with <, and the middle is a number of arbitrary characters, and the end is >. So how does this order change? Leave the students to think about practice.

sed command in Linux

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.