当前位置:网站首页>"Forget to learn again" shell Basics - 29. Awk built-in variables

"Forget to learn again" shell Basics - 29. Awk built-in variables

2022-06-10 11:31:00 Flourishing

1、AWK Built-in variables

AWK The built-in variables are shown in the following table :

awk Built-in variables effect
$0 Represents the present AWK Read the entire row of data . We know AWK Read data line by line ,$0 It represents the entire row of data currently read into the row .
$n It stands for the No. of the current reading line n A field .
NF Fields owned by the current row ( Column ) total .
NR At present AWK Rows processed , Is the row number of the total data .
FS User defined separator .AWK The default delimiter for is any space (tab Key or space ), If you want to use other delimiters ( Such as “:”), Need FS Variable definitions .
ARGC Number of command line parameters .
ARGV Array of command line arguments .
FNR The current number of records in the current file ( The input file starts with 1).
OFMT The output format of the value ( The default is %.6g).
OFS Separator for output field ( Default is space ).
ORS Output record separator ( The default is line break ).
RS Enter record separator ( The default is line break ).

2、 Exercise instructions

(1)$n Variable practice

Use the following text :

ID      Name    Python  Linux   MySQL   Java
1       Tangs   88      87      86      85.55
2       Sunwk   99      98      97      96,66
3       Zhubj   77      76      75      74.44
4       Shahs   66      65      64      63.33

For example, we can extract the number in the text 2 Column data , Execute the following command :

[[email protected] tmp]# awk '{printf $2 "\n"}' student.txt
Name
Tangs
Sunwk
Zhubj
Shahs

(2)FS Variable practice

AWK The default delimiter for is any space (tab Key or space ), If you want to use other delimiters , Need FS Variable definitions .

cut The order defaults to tab Key as separator .

We used cut Command extracted /etc/passwd User name of ordinary user in the file , Now let's use AWK To extract the user name of the user who can log in normally .

Execute the following command :

[[email protected] tmp]# cat /etc/passwd | grep "/bin/bash" | awk '{FS=":"} {printf $1 "\n"}'
root:x:0:0:root:/root:/bin/bash
user1
user2

explain :FS Variable specifies that the delimiter is a separate action , Printout is another action .

Looking at the above results, we will find ,user1 and user2 The user's information is correctly extracted , And the first line root User information , Is to print out the whole line of data , I didn't press : The colon is used as a separator to correctly extract .

Because AWK First read a row of data into AWK in , And then use the following action , Then process the read data .

That is to say, I have put the first line of root User information , Read in to awk in ,$0$1$2 And other variables have been assigned , Only then did I see that you specified in the action behind : Colon as separator , At this time, it is too late to process the first row of data , Only use AWK The default treatment , Use spaces as separators to handle , There are no spaces on this line , All the data in this row will be printed out .

It's time to process the second row of data ,AWK Already know to use : Colon as separator , At this time, the data can be processed correctly .

What you need now BEGIN To deal with this problem , Specify the separator {FS=":"} Put in BEGIN Medium will do .

[[email protected] tmp]# cat /etc/passwd | grep "/bin/bash" | awk 'BEGIN{FS=":"} {printf $1 "\n"}'
root
user1
user2

In this way, we can correctly extract the data we need .

So it's using AWK When , If you need to specify the separator manually , The action specifying the delimiter is written in BEGIN in .

summary : If there is an obvious separator , Recommended priority cut command , Because of the simple .

But if you need some judgment , For example, I need to according to the user ID, View the user name of a user .

Use this time AWK A lot of convenience ,cut Commands cannot be handled directly , You need to write a script to filter .

Like printing uid=500 User name of the user for , The order is as follows :

[[email protected] tmp]# cat /etc/passwd | grep "/bin/bash" | awk 'BEGIN{FS=":"} $3=="500" {printf $1 "\n"}'
user1

# $3=="500" Or you could write it as $3==500 perhaps $3=/500/ Fine 

(3)NF Variables and NR Variable practice

We still use /etc/passwd Take the contents of the file as an example , Print out the user name of the user who can log in , user ID, Line number , Number of fields ( That's the number of columns ).

Execute the following command :

#  Tips : It's written in , The output format is in double quotation marks , Variables are outside double quotation marks .
[[email protected] tmp]# cat /etc/passwd | grep "/bin/bash" | awk 'BEGIN{FS=":"} {printf $1 "\t" $3 "\t  Line number :" NR "\t  Number of fields :" NF "\n"}'
root    0     Line number :1     Number of fields :7
user1   500   Line number :2     Number of fields :7
user2   501   Line number :3     Number of fields :7

Pay attention to the , Finally, it was introduced into AWK Only three lines of data are processed in , as follows :

[[email protected] tmp]# cat /etc/passwd | grep "/bin/bash"
root:x:0:0:root:/root:/bin/bash
user1:x:500:500::/home/user1:/bin/bash
user2:x:501:501::/home/user2:/bin/bash

3、 summary :

We will learn before 5 individual AWK Built in variables are OK , The back AWK Built in variables are generally unavailable , In other words, you can use Shell To deal with the , Use less AWK Handle , If you really need it in the future AWK Deeper programming , I will study it alone AWK.

原网站

版权声明
本文为[Flourishing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101115059360.html