Sed: common linux tools and common sedlinux tools

Source: Internet
Author: User

Sed: common linux tools and common sedlinux tools

Reference: http://www.thegeekstuff.com

Sed Working Principle

This is called as one execution cycle. Cycle continues till end of file/input is reached:
1. Read a entire line from stdin/file.
2. Removes any trailing newline.
3. Places the line, in its pattern buffer.
4. Modify the pattern buffer according to the supplied commands.
5. Print the pattern buffer to stdout.

Command Format:

ADDRESS (row number ):

n: Match the nth line
m,n: From m to n rows
m~n: Starting from m, every n rows
'$': Last line

PATTERN (string ):

/sometext/: Match the line with a sometext string

Sed-n'address 'P filename // print, e. g: sed-n'4' p text.txt. 'Addresss': it is also possible to put p in single quotes, or to replace single quotes with double quotes "ADDRESSp. Sed-n'/PATTERN/P' filename // e. g: sed-n'/aa/P' text.txt sed-n' ADDRESS 'd filename/delete. The row is not deleted, but is deleted from the standard output. Sed-n'/PATTERN/d' filenamesed 'ADDRESS a \ some text' filename // append, add "some text" to the next line of the matched row ", note that single quotes sed '/PATTERN/a \ some text' filenamesed 'address I \ some text' filename // insert, insert a row of sed '/PATTERN/I \ some text' filenamesed' ADDRESS c \ some text' filename // Replace the matched row sed 'pattern c \ some text' filenamesed-n 'address' = filename // print the matched row number, only one ADDRESS is accepted. If multiple rows are printed, use the following format: sed-n 'address,/PATTERN/{= p} 'filename.
Sed Regular Expression search and replacement:

's'The command should be the most important command in sed. The syntax is as follows:

sed 'ADDRESSs/REGEXP/REPLACEMENT/FLAGS' filenamesed 'PATTERNs/REGEXP/REPLACEMENT/FLAGS' filename

Example:

Sed '1s/aa/bb/'text.txt // replace aa with bb, replace sed '2s/aa/bb/'text.txt // replace aa with bb in the first place where aa appears, replace sed's/aa/bb/G' text.txt // with bb in the second place where aa appears. g (global) means global replacement. If you do not add g, it is also a global replacement by default. Sed-n's/aa/bb/gpw result.txt 'text.txt // g replacement, P print, W replace into result.txt file sed's /... $ // 'text.txt // regular expression match: Replace the first three characters in the last position with null, that is, delete the last three characters in each line.
Execute multiple sed commands

Sed-e:Use multiple-eOptions to build up a script out of existing parts.
-eOption is optional for sed with single command. sed will execute the each set of command while processing input from the pattern buffer

-eScript,--expression= Script

    add the script to the commands to be executed

Example:
sed -e 's/foo/bar/' -e '/FOO/d'
Explanation: Use bar to replace foo, and then delete the row with the string FOO.

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.