Linux:awk RS, ors and FS, OFS

Source: Internet
Author: User

awk RS, ors and FS, OFS

Rs:record Separator, Record delimiter

Ors:output record separate, output current record delimiter

Fs:field Separator, Field delimiter

Ofs:out of field Separator, output fields delimiter

Ps:rs, ORS, FS, OFS's English explanation is by no means the case, here just explain clearly. It is recommended to read Awk's English reading, which explains the meaning of abbreviations.

What is field (field) and what is a record (row of records)?

Example:

1.txt

    1. I am a student.
    2. I like to swim
    3. Hello Moto

1 represents the first record line, 2 for the second record line, and 3 for the third record row. By observing we can know that there are 3 record lines in total.

Look at the first line: "I am a student", each word in this line is a field. "I" is a field, "AM" is a field, "a" is a field, "Student" is a field, there is a total of 4 fields in the row.

RS and ORS

RS: Record Line separator

Example:

1.txt

    1. a\n
    2. b\n
    3. c\n
    4. d\n
    5. e\n

The text has a total of 5 lines, and each line has a newline character "\ n". So each row of records is marked with "\ n" as a (newline) flag.

You can use this method to understand:

Find the so-and-so flag, and make each and every content back into a row

Example

1.txt

A|b|c

Code: awk ' begin{rs= ' | ';} {Print $} '

A

B

B

ORS: Can be regarded as the reverse process of RS

Example

1.txt

A

B

C

This can be understood as:

Observe the newline symbol for each line, and replace the newline symbol with the symbol you want.

awk ' begin{ors= '----'} {print $} ' 1.txt

A----b----c----

FS: Field delimiter

The FS default value is "(space)", such as "Hello Moto".

There is a space in "Hello Moto", where the space is the delimiter for Hello and moto (separator), while Hello and Moto are fields (files). Awk is distinguished by a space.

Looking at "I----love----You", if we use the command "awk" {print $} "" You will see the result as:

I----love----

If you want to print out three letters, you can see "----" as a delimiter by observing.

awk ' begin{fs= "----";} {print $1,$2,$3} ' filename

I love You

OFS: The field delimiter for the output.

So to explain, as in the above example "I----love----You", "----" for the delimiter (FS), if we want to change with other symbols to display can do this:

awk ' begin{fs= "----", ofs= "* * * *"} {print $1,$2,$3} ' filename

I*****love*****you

Actually, there's an example of OFS.
echo "ABC" | awk ' {ofs= '. "} {nf=nf; print nf,$0} '
Results
1.abc

Ps:rs and Ors can be said to be an inverse process (↔) can also be seen as a replacement process, but as the process of reciprocal inversion is better understood; FS and OFS are a replacement process.

Turn from

Http://www.cnblogs.com/fhefh/archive/2011/11/16/2251656.html

Linux:awk RS, ors and FS, OFS

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.