Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use awk built-in variables under Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use awk built-in variables under Linux. It is very detailed and has a certain reference value. Interested friends must finish reading it!

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

FILENAME:

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 variables as follows: $awk'{print FILENAME}'~ / domains.txt

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. Output file contents when we use the cat command to view the file domains.txt, we will find that it has 14 lines of text and 2 blank lines: $cat ~ / domains.txt

Awk counts rows:

$awk 'END {print "Number of records in file is:", NR}' ~ / domains.txt

Number of fields in the awk statistics file:

$awk'{"Record:", NR, "has", NF, "fields";}'~ / names.txt

FS built-in variables:

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 do this: * using FS built-in variables, and the second is using the-F option of awk. Look at the / etc/passwd file on the Linux system, where the fields are separated by a colon (:), so when we want to filter out some fields, we can specify the colon (:) as the new input field separator, and awk filters the fields in the password file. We can use the-F option, as follows: $awk-favored rig'{print $1, $4;}'/ etc/passwd

In addition, we can also take advantage of the FS built-in variables as follows: $awk 'BEGIN {FS= ":";} {print $1, $4;}' / etc/passwd

Use OFS built-in variables:

Use the OFS built-in variable to specify a field delimiter for output, which defines how to use the specified characters to separate the output fields using the delimiter of the awk output: $awk-favored BEGIN 'BEGIN {field "= >";} {print $1, $4;}' / etc/passwd

The above is all the contents of the article "how to use awk built-in variables under Linux". Thank you for reading! Hope to share the content to help you, more related 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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report