Linux: Bulk Modify Separators (awk, BEGIN, FS, OFS, print commands)

Source: Internet
Author: User

Bulk Modify the delimiter of the file, you can use the FS and OFS commands

Fs:field Separator, Field delimiter

Ofs:out of field Separator, output fields delimiter

Suppose there is such a file file1.txt, which reads as follows:

As you can see, the file1 delimiter is very long and consists of more than one space character, so we need to unify the delimiter first and enter the command:

Awk-f "" ' {if ($1~/^16/) print $1,$2,$3,$4} ' file1.txt > File2.txt

Generate file2.txt, such as slices:

The delimiter is unified to a single space, then the delimiter is uniformly modified to a comma ",", enter the command:

awk ' begin{fs= ' "; ofs=", "} {print $1,$2,$3,$4} ' file2.txt > File3.txt

The resulting file file3.txt is as follows:

If you want to change the delimiter to a colon, simply change the ofs= "," to ofs= ":"

Linux: Bulk Modify Separators (awk, BEGIN, FS, OFS, print commands)

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.