Examples of using SED file processing tools in Linux

Source: Internet
Author: User

Sed is a good file processing tool, itself is a pipe command, mainly in the behavior of the unit, you can replace, delete, add, select and other specific work.

Grammar

sed [-options] ' command ' filename
Options

-N: Use Quiet (silent) mode. In general sed usage, all data from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specifically handled by SED will be listed.

-E: SED action edit directly on instruction column mode;

-F: Directly write the action of SED in a file, the-f filename can perform the SED action in filename;

-r:sed's actions support the syntax of extended formal notation. (Presupposition is the basic formal representation of French law)

-I: Directly modify the contents of the file read, not by the screen output.


Command

A: New, a can be followed by a string, and these strings will appear on the new line (the current line) ~

C: Replace, C can be followed by strings, these strings can replace the line between N1,N2!

D: Delete, because it is deleted ah, so d usually do not pick up any of the drums;

I: Insert, I can be followed by strings, and these strings will appear on a new line (the current line);

P: Print, that is, to print out a selection of data. Normally p will work with the parameter Sed-n ~

S: Replace and can be directly substituted for work miles! Usually this s action can be paired with formal notation! For example, 1,20s/old/new/g is!
Working with instances

Delete a row

# sed ' 1d ' ab #删除第一行
# sed ' $d ' ab #删除最后一行
# sed ' 1,2d ' ab #删除第一行到第二行
# sed ' 2, $d ' AB #删除第二行到最后一行

Print a line

# sed-n ' 1p ' ab #显示第一行
# sed-n ' $p ' ab #显示最后一行
# sed-n ' 1,2p ' ab #显示第一行到第二行
# sed-n ' 2, $p ' AB #显示第二行到最后一行

Using patterns for querying

# sed-n '/ruby/p ' ab #查询包括关键字ruby所在所有行
# sed-n '/\$/p ' AB #查询包括关键字 $ all row, using backslash \ Mask Special meaning

Add one or more lines of string

# sed ' 1a drink tea ' ab #第一行后增加字符串 "Drink Tea"
# sed ' 1,3a drink tea ' ab #第一行到第三行后增加字符串 "Drink Tea"
# sed ' 1a drink tea\nor coffee ' ab #第一行后增加多行, using line breaks \ n

Replace one or more lines

# sed ' 1c Hi ' AB #第一行代替为Hi
# sed ' 1,2c Hi ' ab #第一行到第二行代替为Hi

Replace a part of a row

# sed-n '/ruby/p ' ab | Sed ' s/ruby/bird/g ' #替换ruby为bird
# sed-n '/ruby/p ' ab | Sed ' s/ruby//g ' #删除ruby

Insert

# sed-i ' $a bye ' ab #在文件ab中最后一行直接输入 ' bye '

Delete a matching row

Sed-i '/ruby/d ' filename #删除带有ruby的行

All right, that's it. The cloud Habitat Community small partners for you to organize on the Linux sed command on the file operation of some methods.

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.