Differences and links between RS, ORS, FS, ofs in awk summary _linux Shell

Source: Internet
Author: User

Learn awk, you must remember to practice, only in practice to find problems, the following on my experience in learning and practice, summed up the difference between rs,ors,fs,ofs and contact.

One, RS and ors

1,rs is the record delimiter , the default delimiter is \ n, specific usage look down

Copy Code code as follows:

[Root@krlcgcms01 mytest]# cat test1//test file
111 222
333 444
555 666

2,rs default delimiter \ n

Copy Code code as follows:

[Root@krlcgcms01 mytest]# awk ' {print $} ' test1//awk ' begin{rs= ' \ n '}{print $} ' test1 These two are the same
111 222
333 444
555 666

In fact, you can understand the contents of the above Test1 file, 222\n333 444\n555 6666, using \ n for segmentation. Take a look at the next example

3, Custom RS Separator

Copy Code code as follows:

[Zhangy@localhost test]$ Echo ' 222|333 444|555 666 ' |awk ' begin{rs= ' |} {Print $0,rt} '
111 222 |
333 444 |
555 666

Combining the above example, it is easy to understand the use of Rs.

4,rs may also be regular expressions

Copy Code code as follows:

[Zhangy@localhost test]$ Echo ' 222a333 444b555 666 ' |awk ' begin{rs= ' [a-z]+ '}{print $1,rs,rt} '
[A-z]+ A
333 [a-z]+ b
555 [a-z]+

From Example 3 and Example 4, we can find that when RT is used to match the content of Rs. If RS is a fixed value, RT is the content of Rs.

When 5,rs is empty

Copy Code code as follows:

[Zhangy@localhost test]$ cat-n test2
1 111 222
2
3 333 444
4 333 444
5
6
7 555 666
[Zhangy@localhost test]$ awk ' begin{rs= ' "}{print $} ' test2
111 222
333 444
333 444
555 666
[Zhangy@localhost test]$ awk ' begin{rs= ';} {print "<", $, ">"} ' test2//This example looks more obvious
< 222 >
< 333 444//This line and the line below, is a line
333 444 >
< 555 666 >

From this example, you can see that when RS is empty, awk automatically acts as a delimiter in multiple lines.

6,ors Record output is character , the default value is \ n

The Ors is understood to be an RS reverse process, which makes it easier to remember and understand, looking at the example below.

Copy Code code as follows:

[Zhangy@localhost test]$ awk ' begin{ors= ' \ n '}{print $} ' test1//awk ' {print $} ' test1 the two are the same
111 222
333 444
555 666
[Zhangy@localhost test]$ awk ' begin{ors= ' |} {print $} ' test1
111 222|333 444|555 666|

Second, FS and OFS

1,fs Specify column delimiters

Copy Code code as follows:

[Zhangy@localhost test]$ echo "111|222|333" |awk ' {print} '
111|222|333
[Zhangy@localhost test]$ echo "111|222|333" |awk ' begin{fs= "|"} {Print $} '
111

2,fs can also use regular

Copy Code code as follows:

[Zhangy@localhost test]$ echo 111| | 222|333 "|awk ' begin{fs=" [|] + "}{print $}"
111

When the 3,fs is empty

Copy Code code as follows:

[Zhangy@localhost test]$ echo "111|222|333" |awk ' begin{fs= ""}{nf++;p rint $} "
1 1 1 | 2 2 2 | 3 3 3

When FS is empty, awk treats each character in a line as a column.

when 4,rs is set to non \ n , \ n will be one of the FS delimiters

Copy Code code as follows:

[Zhangy@localhost test]$ Cat Test1
111 222
333 444
555 666
[Zhangy@localhost test]$ awk ' begin{rs= "444";} {print $2,$3} ' test1
222 333
666

Between 222 and 333 there is a \ n, when the RS set to 444, 222 and 333 are identified as the same row of two columns, in fact, according to the conventional thinking is a column of two rows.

5,ofs Column Output Separator

Copy Code code as follows:

[Zhangy@localhost test]$ awk ' begin{ofs= ' | ';} {print $1,$2} ' test1
111|222
333|444
555|666
[Zhangy@localhost test]$ awk ' begin{ofs= ' | ';} {print $ OFS $} ' test1
111|222
333|444
555|666

Test1 is only two columns, if 100 columns, it's too much trouble to write it.

Copy Code code as follows:

[Zhangy@localhost test]$ awk ' begin{ofs= ' | ';} {print $} ' test1
111 222
333 444
555 666
[Zhangy@localhost test]$ awk ' begin{ofs= ' | ';} {Nf=nf;print $} ' test1
111|222
333|444
555|666

Why does the OFS in the second method take effect? Personally, Awk found that when the column changed, it would allow OFS to take effect, not change the direct output.

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.