Usage of the Linux sed command

Source: Internet
Author: User

Original http://blog.chinaunix.net/uid-24426415-id-77244.html

-------------------------------------------------------------------------------

Beginner's entry

the SED tool is a non-interactive stream editor. By default, only the output is affected and the input is not changed . An SED is a unit of behavior when processing a document. Features are: Delete, find replace, add , insert, read from other files.

In fact, these functions seem to be implemented with vim and other editors. So, why do we have sed?

main application Scenarios
    • Too large a text
    • Regular text changes

Command format for SED

...]
    • 1
    • 1
Delete
#删除第一行sed ‘1d‘ file
    • 1
    • 2
    • 1
    • 2

Note that this only affects the output stream. If you want to save it,

-i ‘1d‘ filename
    • 1
    • 1

Or

#输出到新文件sed ‘1d‘>newfilename
    • 1
    • 2
    • 1
    • 2

where the 1d command, we call 1 address, here refers to the first line.
Delete First line to last row

sed ‘1,$d‘ filename
    • 1
    • 1

Delete a line that contains a pattern

‘/pattern/d‘ filename#例如sed ‘/^$/d‘ filename
    • 1
    • 2
    • 3
    • 1
    • 2
    • 3
Find Replacements
#普通替换 将每行的第一个line替换成LINEsed ‘s/line/LINE/‘ filename
    • 1
    • 2
    • 1
    • 2

Sed ' s/line/line/[number]
Represents the maximum number of lines to replace in this line, and if number is G, replace all

Character Conversions
现在还没有见过重要的用法
    • 1
    • 1
Insert Text
#在第二行前插入一行sed ‘2 i insert_context‘ filename#在第二行之后插入一行sed ‘2 a insert_context‘ filename#在匹配的行之前插入一行sed ‘/pattern/i\new_word‘ filename
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

Print

#只打印出第一行 ,不加n的话会默认输出每一行sed -n ‘1p‘ filename#只打印出被修改的一行sed -n ‘s/the/THE/p‘ filename

Usage of the Linux sed command

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.