In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.