Linux Basic command SED

Source: Internet
Author: User

Sed (stream editor) Linux Three Musketeers, stream editor, to achieve the deletion of files and replace the search

Parameter-n cancels the default output-I modify Content-E allows multiple edits, p printing

Sed-n ' $ ' p ett,txt $ is the meaning of the line end, indicating that the last line is printed

When G is used in conjunction with S, it represents the replacement of the current global match

S often said to find and replace, with one string replaced by another

Sed-i s#oldboy#oldgirl#g A.txt # is a delimiter



Sed is a good file processing tool, itself is a pipe command, mainly in the behavior of the unit processing, you can replace the data rows, delete, add, select and other specific work, the following first understand the use of SED
The SED command line format is:
sed [-nefri] ' command ' input text

common options:
         

common commands:
        a  : New, a can be followed by a string, and these strings will appear on a new line (the current next line) ~
        c   : Replace, C can be followed by string, these strings can replace the line between N1,N2!
        d  : Delete, because it is deleted, so D usually does not take any of the following;
          i  : Insert, I can be followed by a string, and these strings will appear on a new line (the current line);
          p  : Print, that is, a selection of information printed. Normally p works with parameter Sed-n ~
         s  : Replace, can be directly replaced by work! Usually this s action can be paired with formal notation! For example 1,20s/old/new/g is!

example : (assuming we have a file named ab)
      Delete a row
      [[email protected] Ruby # sed ' 1d ' ab                #删除第一行  
     [[email protected] ruby] # sed ' $d ' ab                #删除最后一行
     [[email protected] ruby] # sed ' 1,2d ' ab             #删除第一行到第二行
     [[ Email protected] Ruby] # sed ' 2, $d ' AB              #删除第二行到最后一行

Show a line
.    [[email protected] Ruby] # sed-n ' 1p ' ab             #显示第一行  
     [[email protected] ruby] # sed-n ' 1,2p ' ab          #显示第一行到第二行
     [[email protected] ruby] # sed -N ' 2, $p ' ab         #显示第二行到最后一行

Querying using a pattern
[[email protected] ruby] # sed-n '/ruby/p ' ab #查询包括关键字ruby所在所有行
[[email protected] ruby] # sed-n '/\$/p ' AB #查询包括关键字 $ where all lines, using backslashes \ Shielding special meaning

Add one or more lines of string
[email protected] ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
[[email protected] ruby]# sed ' 1a drink tea ' ab#第一行后增加字符串 "Drink Tea"
Hello!
Drink tea
Ruby is me,welcome to my blog.
End
[[email protected] ruby]# sed ' 1,3a drink tea ' ab#第一行到第三行后增加字符串 "Drink Tea"
Hello!
Drink tea
Ruby is me,welcome to my blog.
Drink tea
End
Drink tea
[[email protected] ruby]# sed ' 1a drink tea\ nor coffee ' ab#第一行后增加多行, use line break \ n
Hello!
Drink tea
or coffee
Ruby is me,welcome to my blog.
End

Instead of one or more lines
     [[email protected] ruby] #第一行代替为Hi
     Hi
     Ruby is me,welcome to my blog.
     end
     [[email protected] Ruby] # sed ' 1,2c Hi ' ab              #第一行到第二行代替为Hi
     Hi
     End

Replace part of a line
format: sed "s/ string to replace / new string /g '    (the string to be replaced can be used as a regular expression)
      [[email protected] Ruby] # sed-n '/ruby/p ' ab | sed ' s/ruby/bird/g '     #替换ruby为bird
  [[email protected] ruby] # Sed-n '/ruby/p ' ab | Sed ' s/ruby//g '         #删除ruby

Insert
[[email protected] ruby] # sed-i ' $a bye ' ab #在文件ab中最后一行直接输入 "Bye"
[email protected] ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
Bye

Delete a matching row

Sed-i '/Match string/d ' filename (note: If the match string is a variable, you need "" instead of ". Remember as if it were)

Replace a string in a matching row

Sed-i '/Match string/s/Replace source string/Replace target string/g ' filename


Linux Basic command SED

Related Article

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.