Examples of common usage techniques for SED

Source: Internet
Author: User

1.sed replacing line breaks and label usage

echo "A,b,c,d" |sed ' s/,/\n/g ' |sed ' s/\n/,/g '

Parsing: The above command does not implement the replacement line character, which is related to the line processing of sed, sed read a line, will first take the line break, after processing and then add, so it is not possible to use the above command to replace the newline character, You must use the other commands in SED to complete. The above recovery can use the TR command:

echo "A,b,c,d" |sed ' s/,/\n/g ' |tr-t ' \ n ', '

The following command implements all line breaks in the replacement text:

Sed ': label; N;s/\n/:/;b label ' filenamesed ': label; N;s/\n/:/;t label ' filename

Analytical:

: label; This is a tag, used to implement the jump processing, the name can be arbitrarily taken (label), the following B label is the jump command

N n is a processing command for the SED, appending the next line in the text stream to the pattern space for merging, so that line breaks are visible

s/\n/:/; S is the Replace command for SED, replacing newline characters with colons

b label or T label b/t is an sed jump command that jumps to the specified label

2.sed output odd and even rows

A.

Sed-n ' p;n ' file #奇数行sed-n ' n;p ' file #偶数行

Parse: n means reading the next input row and processing the new row with the next command instead of the first command.

B.

Sed-n ' 1~2p ' file #奇数行sed-n ' 2~2p ' file #偶数行

Parse: The first number represents the starting line, and the second number represents the stride.

Resources:

http://my.oschina.net/shelllife/blog/118337

Examples of common usage techniques for SED

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.