In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use awk built-in variables. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
We will gradually unravel the mystery of awk functionality, and in this section, we will introduce the concept of awk built-in built-in variables. You can use two types of variables in awk: user-defined user-defined variables and built-in variables.
Awk built-in variables already have predefined values, but we can modify these values carefully. Awk built-in variables include:
FILENAME: the current input file name
NR: the current input line number (refers to input line 1pm 2pm 3... Etc.)
NF: the field number of the current input line
OFS: output field delimiter
FS: enter field delimiter
ORS: output record delimiter
RS: enter record delimiter
Let's continue to demonstrate some ways to use the above awk built-in variables:
To read the name of the current input file, you can use the FILENAME built-in variable, as follows:
$awk'{print FILENAME}'~ / domains.txt
Awk FILENAME variable
You will see that each line will output a file name, which is the default behavior of awk when you use the FILENAME built-in variable.
We can use NR to count the number of lines (records) in an input file, keeping in mind that it also counts blank lines, as we will see in the following example.
When we use the cat command to view the file domains.txt, we find that it has 14 lines of text and 2 blank lines:
$cat ~ / domains.txt
Output file contents
$awk 'END {print "Number of records in file is:", NR}' ~ / domains.txt
Awk counts rows
To count the number of fields in a record or row, we can use the NR built-in variable as follows:
$cat ~ / names.txt
List the contents of the file
$awk'{"Record:", NR, "has", NF, "fields";}'~ / names.txt
The number of fields in the awk statistics file
Next, you can also use the FS built-in variable to specify an input file delimiter that defines how awk divides the input line into fields.
The default values for FS are "spaces" and "tabs", but we can also change the FS value to any character to have awk split the input line as appropriate.
There are two ways to achieve this:
* one way is to use FS built-in variables
The second method is to use the-F option of awk
Take a look at the / etc/passwd file on the Linux system, where the fields are separated by:, so when we want to filter out some fields, we can specify: as the new input field delimiter, as shown in the following example:
We can use the-F option as follows:
$awk-fanglu'{print $1, $4;}'/ etc/passwd
Awk filters fields in password files
In addition, we can also take advantage of FS built-in variables, as follows:
$awk 'BEGIN {FS= ":";} {print $1, $4;}' / etc/passwd
Use awk to filter fields in a file
Use the OFS built-in variable to specify a field delimiter for output, which defines how the output fields are separated by the specified characters, as shown in the following example:
BEGIN {OFS= "= >";} {print $1, $4;}'/ etc/passwd
Add delimiters to fields in the file
Thank you for reading! This is the end of this article on "how to use awk built-in variables". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.