awk changes the input and output delimiter instance analysis

Source: Internet
Author: User

awk accepts data from stdin by default, printing text to stdout.

Default input and output delimiters for awk:

FS: Enter field delimiter, default space.

RS: Enter line delimiter, default \ n.

OFS: Output field delimiter, default space.

ORS: Output line delimiter, default \ n.

Example: Make multiple rows into a row by changing the default built-in variable

File.txt (the number of information bars for each person is not sure, the distinction is a blank line)

Pedestrian Armor

Tel: 13777707771

Mobile: 010-12345678

Passers-by B

Tel: 13912344321

Mobile: 010-56784321

qq:87654221

To change the format to:

Passers-by phone: 13777707771 Mobile: 010-12345678

Passerby B Tel: 13912344321 Mobile: 010-56784321 qq:87654221

Give the final order first, then look at the analysis.

awk ' begin{fs= ' \ n '; Rs= ""; ors= ""}{for (x=1;x<=nf;x++) {print $x "\ T"} print "\ n"} './t.txt

Analysis:

Change FS to \ n so that read into the domain is a line, and then change the RS to "", so that a person's information read into a line. Now it's two rows of data. The next step is the output.

Because the default OFS is the space, Ors is \ n, that's what we want, so

' begin{fs= "\ n"; Rs= ""}{print $1,$2,$3,$4}' ./t.txt

The result of the output is:

Passers-by phone: 13777707771 Mobile: 010-12345678

Passerby B Tel: 13912344321 Mobile: 010-56784321 qq:87654221

That's what we want, but it's not a good idea to output to $4, assuming that a person's record is more than 4 rows.

At this point I want to print the $1,$2,$3,$4 directly, and print is not the same, but the result is:

' begin{fs= "\ n"; Rs= ""}{print $}' ./t.txt

Pedestrian Armor
Tel: 13777707771
Mobile: 010-12345678
Passers-by B
Tel: 13912344321
Mobile: 010-56784321
qq:87654221

It's a little different from what you want, why? because OFS does not work when the output is $, OFS is only used when outputting multiple fields to be inserted between each domain.

Then I thought I'd use a for loop to print out every field in each row. So

' begin{fs= "\ n"; Rs= ""}{for (x=1;x<=nf;x++) {print $x}}' ./t.txt

Pedestrian Armor
Tel: 13777707771
Mobile: 010-12345678
Passers-by B
Tel: 13912344321
Mobile: 010-56784321
qq:87654221

The result seems to have changed, because each print for the for loop will output a ORS, and you can specify a visible ors to try and know.

So you need to specify ORS as "" and manually output line breaks. This is the very beginning of the order.

awk changes the input and output delimiter instance analysis

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.