Sed command notes

Source: Internet
Author: User

Tag: Linux sed Regular Expression replacement

The format of line breaks for DOS/Windows and Linux/UNIX files is different. DOS/Windows-based text files have a Cr (line breaks) and LF (line breaks) at the end of each line ), UNIX text only has one line feed.
1) Move the files in DOS/Windows to Linux/Unix

Although manyProgramWe don't care about the DOS/Windows format Cr/LF text files, but there are several programs that care about it-the most famous is Bash. As long as we press enter, it will cause problems. The following sed calls convert DOS/Windows text to a trusted UNIX format:

$ Sed-E's/. $ // 'mydos.txt> myunix.txt

Replace the last character of each line with null

$ Sed-E's/. $/ABC/'mydos.txt> myunix.txt

Replace the last character of each line with ABC.

The script works very easily: the replacement rule expression matches the last character of a row, and the character is exactly the carriage return. We can replace it with an empty character to completely delete it from the output. If you use this script and notice that the last character of each line in the output has been deleted, you specify a text file that is already in UNIX format. So there is no need to do that!

2) Move the Linux/UNIX text to the Windows system and use the following script to perform the required format conversion:

$ Sed-E's/$/\ r/'myunix.txt> mydos.txt

Add \ r at the end of each line

$ Sed-E's/123 $/345/'myunix.txt> mydos.txt

Replace 123 with 123 for the row ending with 345

In this script, the '$' rule expression matches the end of the row, and '/R' tells sed to insert a carriage return before it. Insert a carriage return before line feed. Immediately, each line ends with Cr/LF. Note that '\ R' is replaced with CR only when GNU sed 3.02.80 or later is used '.

========================================================== ==================================

How to convert a DOS file format to a Unix File Format

When a DOS file is transmitted to a UNIX system, it will end with an extra ^ m at the end of each line. Of course, it may not be visible, but the format of this file will be shown below in VI, for example, "dos.txt" [dos] 120l, 2532c indicates a [dos] format file. If it is a MAC system, [Mac] is displayed. because the file format may sometimes cause errors in our Unix or Shell programs, you need to convert these DOS file formats to the Unix format by using the following method:
VI dos.txt
: Set fileformat = Unix
: W
In this way, the file will be converted to a UNIX file. This may happen if the file is written on a Windows machine and uploaded to Unix.
Use the command: Set ff?
You can see the DoS or Unix words.
Use: Set FF = UNIX to force it to Unix format
You can also use a tool like sed:
Sed's/^ m // 'filename> tmp_filename
^ M is simultaneously pressed by Ctrl + V + M, indicating the carriage return.

Differences in DOS and Unix format processing: DOS is carriage return + line feed, while in UNIX, only carriage return, no line feed!

Sed-E's/a/A/'t.txt> t1.txt converts the first a in each line of t.txt to

Sed-E's/a/A/G' t.txt> t1.txt converts all A in each line of t.txt to

3) how to mix windows and Unix formats into UNIX format

Sed-E's/\ r // G' then upload TXT> unix.txt

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.