The Linux shell uses the SED command to add characters at the end of a line or at the beginning of a text

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/aaronwxb/archive/2011/08/19/2145364.html

Writing a script yesterday took 2/3 of the day, and most of the time spent on the SED command, today do not summarize the time of yesterday, I'm sorry.

There are several commands for adding characters at the beginning or end of a line with the SED command:

Assume that the processed text is Test.file

Add characters to the head of each line, such as "head", with the following command:

Sed ' s/^/head&/g ' test.file

Add characters at the end of each line, such as "TAIL", with the following command:

Sed ' s/$/&tail/g ' test.file

Running results such as:

A few notes:

1. "^" represents the beginning of the line, "$" for the end

2. The character G in ' s/$/&tail/g ' represents all occurrences of the character in each line, and if you want to add it at a specific character, G will be useful, otherwise it will only replace the first one in each row, instead of looking backwards.

Cases:

3. If you want to export the file, add "> Outfile_name" at the end of the command, if you want to change it on the original file, adding the option "-I", as

4. It is also possible to add two commands together, adding the character "HEAD", "TAIL" to each line of Test.file, and the command: sed '/./{s/^/head&/;s/$/&tail/} ' test.file

In fact, the above is OK, spend too much time yesterday, mainly because the file is processed by MySQL from the database extracted from the results of the guide, others gave me after I directly deal with, too brain remnants =-I have been a little doubt the result is wrong, there may be Windows and Linux line-wrapping problem, But because of the SED is not familiar, has been engaged in the sed ....

well-known (=-), window and Linux carriage return line of the same, if you know, skip this paragraph, do not know, read:

In Unix systems, the end of each line is only "< newline >", or "\ n", and in Windows, each line ends with "< newline >< Enter >", or "\n\r". A direct consequence of this is that when a file in a Unix system is opened in Windows, all text becomes a line, and the files in Windows are opened under UNIX, which may have a ^m symbol at the end of each line.

Okay, so my question is that there are ^m symbols at the end of each line of the processed file, which is usually not visible. Can be viewed with the "cat-a test.file" command. So when I want to add a character at the end of a line, it is always added at the beginning of the line and overwrites the character at the beginning of the line.

To convert the file, there are two ways:

1. Command Dos2unix Test.file

2. Remove the "\ r" and use the command sed-i ' s/\r//' test.file

OK, so finish, OK!!!

The Linux shell uses the SED command to add characters at the end of a line or at the beginning of a text

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.