SED replaces line wrapping with spaces or other characters

Source: Internet
Author: User

The SED flow Editor is a great tool for handling text content in the shell. The SED command reads a line of text from a text stream to a corresponding command, or script, in a pattern space, so it is somewhat special when dealing with line breaks.

The following commands can be executed correctly:

The code is as follows Copy Code

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

But if I want to get back, the following command has no effect:

The code is as follows Copy Code

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

This is related to the way sed is handled, and when SED reads a line, it removes the line break and adds it after it is processed, so it is not possible to replace the line break with the command above, and it must be done using the other commands in sed. The above recovery can use the TR command:

The code is as follows Copy Code

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

The following is a number of uses found on the Internet, the effect of the practice is different, only one is completely feasible.

The code is as follows Copy Code

Sed ': label; N;s/n/:/;b label ' filename

Sed ': label; N;s/n/:/;t label ' filename

The above two commands enable you to replace all line breaks in a file with a specified string, such as a colon in a command. Explanation of the Order:

◦:label; This is a label, used to achieve jump processing, the name can be arbitrarily taken (label), the following B label is the jump instruction
◦n; n is a processing command for SED that appends the next line in the text stream to the pattern space for merging, so that the newline character is visible
◦s/n/:/; S is SED's replacement command, replacing a newline character with a colon
◦b label or T label b/t is a sed jump command that jumps to the specified label
Tag jump and N of the additional command to achieve each line of the continuous into the mode of processing space, so that will not miss every line break, and without the label of the word jump, you can only replace each line of a newline character, contrast effect:

The code is as follows Copy Code

$ echo "a,b,c,d" |sed ' s/,/n/g ' |sed ': x; N;s/n/,/;b x '

A,b,c,d

$ echo "a,b,c,d" |sed ' s/,/n/g ' |sed ' n;s/n/,/'

A,b

C,d

There is also the processing effect of one of the following commands and the substitution of line breaks cannot be implemented. In fact, the $ symbol represents the last line in the text stream in sed, and I don't quite understand the following results.

The code is as follows Copy Code

$ echo "a,b,c,d" |sed ' s/,/n/g ' |sed ' s/$/,/'

A

B

C

D

n Command and ~ address representations in ps:sed

The code is as follows Copy Code

Sed ' 8,80{n;n;n;d} ' filename

n indicates that the next line in the text stream is read to the pattern space (n is appended), or only one row is processed by SED. The above command understands: From line 8th (included), once read the 9/10/11 line, and then read the 11 lines, execute the d command to delete the 11th line in the processing space, then start with 12 lines, read 13/14/15, delete 15 lines, and so on, until line 80th.

The code is as follows Copy Code

Sed ' 11~4d ' filename

The command implements the same functionality as the previous command, and the only difference is that it does not specify the end line until the end of the file.

The code is as follows Copy Code
Sed-i-E ': x; n;s/n//;b x ' User_uniq.txt

Implement to replace newline with space

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.