Commonly Used for awk
awk mainly deals with data in the field of each row, and the default field separator is blank key or tab key.
We can use last to retrieve the information of the login person, and the result is as follows(take out only the first 5 lines):
$ last -n 5
jimmy unix:0.0 Tue Nov 9 12:41 still logged in
jimmy unix:0.0 Tue Nov 9 11:54 - 12:40 (00:46)
boot time Tue Nov 9 11:52
shutdown time Tue Nov 9 01:15
jimmy unix:0.0 Mon Nov 8 22:46 - 01:15 (02:29)
jimmy unix:0.0 Tue Nov 9 12:41 still logged in
jimmy unix:0.0 Tue Nov 9 11:54 - 12:40 (00:46)
boot time Tue Nov 9 11:52
shutdown time Tue Nov 9 01:15
jimmy unix:0.0 Mon Nov 8 22:46 - 01:15 (02:29)
I want to retrieve the account number and the day of the week, and the account number and the day of the week are separated by tab key:
$ last -n 5 | awk '{print $1 "\t" $3}'
jimmy Tue
jimmy Tue
boot Tue
shutdown Tue
jimmy Mon
jimmy Tue
jimmy Tue
boot Tue
shutdown Tue
jimmy Mon
The $0 in the first line represents the line "jimmy unix:0.0 Tue Nov 9 12:41 still logged in".
The processing flow of awk is:
1. Read in the first row, and fill in the data of the first row into variables such as $0, $1, $2....
2. According to the restriction of condition type, determine whether the following action is needed.
3. Finish all actions and condition types.
4. If there are subsequent line data, repeat the steps 1~3 above until all the data have been read.
Built-in variables of awk:
NF : The total number of fields in each row ($0).
NR : Currently, what awk is processing is the number of rows data.
FS : The current separator character, the default is the space bar.
Example:
$ last -n 5| awk '{print $1 "\t lines: " NR "\t columns: " NF}'
jimmy lines: 1 columns: 9
jimmy lines: 2 columns: 9
boot lines: 3 columns: 6
shutdown lines: 4 columns: 6
jimmy lines: 5 columns: 9
jimmy lines: 1 columns: 9
jimmy lines: 2 columns: 9
boot lines: 3 columns: 6
shutdown lines: 4 columns: 6
jimmy lines: 5 columns: 9
awk logical operation characters:
> : Greater than
< : Less than
>= : Greater than or equal to
<= : Less than or equal to
== : Equal
!= : Not equal to
Example: Suppose I want to check the data in the third column less than 10, and only list the account number and the third column.
cat /etc/passwd | awk '{FS=":"} $3 < 10 {print $1 "\t" $3}'
or
cat /etc/passwd | awk 'BEGIN {FS=":"} $3 < 10 {print $1 "\t " $3}'
The content of pay.txt is as follows:
Name 1st 2nd 3th
VBird 23000 24000 25000
DMTsai 21000 20000 23000
Bird2 43000 42000 41000
Name 1st 2nd 3th
VBird 23000 24000 25000
DMTsai 21000 20000 23000
Bird2 43000 42000 41000
I wan to calculate the total of each person and also format the output.
$ cat pay.txt |\
> awk 'NR==1{printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total"}
> NR>=2{total = $2 + $3 + $4
> printf "%10s %10d %10d %10d %10.2f\n",$1,$2,$3,$4,total}'
Name 1st 2nd 3th Total
VBird 23000 24000 25000 72000.00
DMTsai 21000 20000 23000 64000.00
Bird2 43000 42000 41000 126000.00
> awk 'NR==1{printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total"}
> NR>=2{total = $2 + $3 + $4
> printf "%10s %10d %10d %10d %10.2f\n",$1,$2,$3,$4,total}'
Name 1st 2nd 3th Total
VBird 23000 24000 25000 72000.00
DMTsai 21000 20000 23000 64000.00
Bird2 43000 42000 41000 126000.00
The {} in the action of awk also supports if (condition) as follows:
$ cat pay.txt |\
> awk '{if(NR==1) printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total"}
> NR>=2{total = $2 + $3 + $4
> printf "%10s %10d %10d %10d %10.2f\n",$1,$2,$3,$4,total}'
Name 1st 2nd 3th Total
VBird 23000 24000 25000 72000.00
DMTsai 21000 20000 23000 64000.00
Bird2 43000 42000 41000 126000.00
> awk '{if(NR==1) printf "%10s %10s %10s %10s %10s\n",$1,$2,$3,$4,"Total"}
> NR>=2{total = $2 + $3 + $4
> printf "%10s %10d %10d %10d %10.2f\n",$1,$2,$3,$4,total}'
Name 1st 2nd 3th Total
VBird 23000 24000 25000 72000.00
DMTsai 21000 20000 23000 64000.00
Bird2 43000 42000 41000 126000.00
About printf.
Comments
Post a Comment