Explanation AWK built in variable LINUX FS NF NR RT RS ORS OFS

  • 2020-06-19 12:33:06
  • OfStack

I have read a lot of articles about AWK built-in variables on the Internet. Personal understanding and memory are added here to help you master the basic usage of these variables.

FS Specifies the field un column separator (Font Space)


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

OFS Specifies the output field column separator (Output Font space)


[~/AWK_learning]$ echo "111 222 333" |awk 'BEGIN{OFS="|";}{print $1,$2,$3}'
111|222|333

RS Specifies the line separator The default delimiter is \n (Row Space)


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

ORS specifies the output line separator


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

RT is the delimiter


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

Total number of fields per row (Number of Font)


[~/AWK_learning]$ cat test.txt
111 222
333 444
555 666
[~/AWK_learning]$ awk '{print NF}' test.txt
2
2
2
[~/AWK_learning]$ awk '{print $NF}' test.txt
222
444
666

NR Current row count (Number of Row)


[~/AWK_learning]$ cat test.txt
111 222
333 444
555 666 777
[~/AWK_learning]$ awk '{print NR}' test.txt
1
2
3
[~/AWK_learning]$ awk '{print $NR}' test.txt
111
444
777

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

NR refers to the number of times the data is read according to the record delimiter after the execution of awk. The default record delimiter is line break, so the default is the number of data rows 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, hence FNR. Every time a new file is processed, FNR counts from 1. FNR can be understood as File Number of Record.

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

The following is an example program to illustrate. First, two input files, class1 and class2, are prepared to record the grade information of the two classes. The contents are as follows:


CodingAnts@ubuntu:~/awk$ cat class1
zhaoyun 85 87
guanyu 87 88
liubei 90 86
CodingAnts@ubuntu:~/awk$ cat class2
caocao 92 87 90
guojia 99 96 92

Now to see all the grade information for both classes and prefix each message with a line number, use the awk command below;


CodingAnts@ubuntu:~/awk$ awk '{print NR,$0}' class1 class2
1 zhaoyun 85 87
2 guanyu 87 88
3 liubei 90 86
4 caocao 92 87 90
5 guojia 99 96 92

The line number here is implemented using NR, which adds 1 to the value of NR for every record read. If you want to change the line number for each class from scratch, you need to do this using FNR, as follows:


CodingAnts@ubuntu:~/awk$ awk '{print FNR,$0}' class1 class2
1 zhaoyun 85 87
2 guanyu 87 88
3 liubei 90 86
1 caocao 92 87 90
2 guojia 99 96 92


Related articles: