sed commands use simple records

Source: Internet
Author: User

Sed ' s/pattern/replace_string/' file

Replace sed ' s/lifeng/lf/' sed.txt with LF for the first lifeng of each line in Sed.txt (the source file content will not change)

To replace all content, command tail plus parameter g sed ' s/lifeng/lf/g' Sed.txt

To match the second content of each line using 2g sed ' s/lifeng/lf/2g' Sed.txt

You can also use Cat Sed.txt | Sed ' s/lifeng/lf/' >file (output redirect)

If you want to directly modify the original file plus parameters –i sed –i ' s/lifeng/lf/' sed.txt

Sed–i. Bak ' s/lifeng/lf/' Sed.txt replaces the source file and creates a File.bak backup file

Sed removal of blank line sed '/^$/d ' sed.txt

blank line available regular ^$     to indicate /pattern/d The row that matches the style will be removed

Matched string token:& indicates matched content

[[email protected] csv]# echo this was an example|sed ' s/\w\+/[&]/g '

[This] [IS] [An] [Example]

[[email protected] csv]# echo this was an example|sed ' s/\w\+/[&]/'

[This] was an example

Regular \w\+ matches each word and then we replace it with [&] & corresponds to the word that was previously matched

Quotes

Sed expressions are generally quoted in single quotes, but they can also be used in double quotes, and double quotation marks can be extended by evaluating an expression, and double quotes are useful when we want to use some variables in an SED expression.

Example: Text=hello

echo Hello word|sed "s/$text/hello/" (single quote not)

HELLO Word

This is to delete the first row to the nth row

Sed-i ' 1,nd ' filename

sed commands use simple records

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.