Get the App
SLTechnology News&Howtos  ›  Servers  › 

Example Analysis of AWK built-in variable FS,NF,NR,RT,RS,ORS,OFS in LINUX

Shulou Source: shulou.com Published: 2022-06-01 17:50:52 09月11日 Update

Editor to share with you the example analysis of the built-in variable FS,NF,NR,RT,RS,ORS,OFS in AWK in LINUX, I believe that most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.

FS specifies the field un column delimiter (Font Space)

[~ / AWK_learning] $echo "111 | 222 | 333" | awk'{print $1} '111 | 333 [~ / AWK_learning] $echo "111 | 222 | 333" | awk' BEGIN {FS= "|"} {print $1} '111 |

OFS specifies the output field column delimiter (Output Font space)

[~ / AWK_learning] $echo "1112222333" | awk 'BEGIN {OFS= "|;} {print $1

RS specifies that the default line delimiter is\ n (Row Space)

[~ / AWK_learning] $echo "111,222 | 333,444 | 555,666" | awk 'BEGIN {RS= "|"} {print $0}' 111 222333 444555 666 "

ORS specifies the output line delimiter

[~ / AWK_learning] $awk 'BEGIN {ORS= "|;} {print $0;}' test.txt111 2222 | 333,444 | 555,666

RT reference delimiter

[~ / AWK_learning] $echo "111,222 | 333,444 | 555,666" | awk 'BEGIN {RS= "|"} {print $0prit RT}' 111222 | 333,444 | 555,666 |

Total fields per row of NF (Number of Font)

[~ / AWK_learning] $cat test.txt111 222333 444555 666 [~ / AWK_learning] $awk'{print NF} 'test.txt222 [~ / AWK_learning] $awk' {print $NF} 'test.txt222444666

Current rows of NR (Number of Row)

[~ / AWK_learning] $cat test.txt111 222333 444555 666 777 [~ / AWK_learning] $awk'{print NR} 'test.txt123 [~ / AWK_learning] $awk' {print $NR} 'test.txt111444777

Let's take a look at the related problems in the execution of built-in variables:

NR indicates the number of data read according to the record delimiter after execution from awk. The default record delimiter is the newline character, so the default is the number of rows of data read. NR can be understood as the abbreviation of Number of Record.

When awk processes multiple input files, after processing the first file, NR does not start at 1, but continues to accumulate, so there is FNR. Every time a new file is processed, FNR counts from 1, and FNR can be understood as File Number of Record.

NF represents the number of fields in which the current record is divided, and NF can be understood as Number of Field.

The following is illustrated by an example program. First, two input files, class1 and class2, are prepared to record the score information of the two classes, as shown below:

CodingAnts@ubuntu:~/awk$ cat class1zhaoyun 85 87guanyu 87 88liubei 90 86 CodingAntsThe UBUTUBUTULTUBUBUTULTUBY AWK $cat class2caocao 92 87 90guojia 99 96 92

Now to view all the score information for both classes and precede each with a line number, you can use the following awk instruction

CodingAnts@ubuntu:~/awk$ awk'{print NR,$0} 'class1 class21 zhaoyun 85 872 guanyu 87 883 liubei 90 864 caocao 92 87 905 guojia 99 96 92

The line number here is implemented through NR, and each time awk reads a record, the value of NR is incremented. If you require the line number of each class to change from scratch, you need to use FNR to do so, as follows:

CodingAnts@ubuntu:~/awk$ awk'{print FNR,$0} 'class1 class21 zhaoyun 85 872 guanyu 87 883 liubei 90 861 caocao 92 87 902 guojia 99 9692 is all the contents of the article "sample Analysis of the built-in variable FS,NF,NR,RT,RS,ORS,OFS in AWK in LINUX". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Delimiters fields files variables examples two information content class article line number processing analysis that is grades data time input output not much Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno vpn MySQL Shulou Technology