Awk learning practices

Source: Internet
Author: User
1. what kind of shell does instance 1 make the $2 of file1 and file2 different and get the global $0? Note: When comparing the $2 files, make sure that the first column is the same before comparing the 1.1 files and the operation file # catfile000012000002300003040000456000...

1. instance 1
What kind of shell can make the $2 of file1 and file2 different and get the global $0?
Note: When comparing the $2 files, you must compare them only when the first column is the same.
1.1 Operating files
# Cat file1
00001 20
00002 31
00003 04
00004 56
00005 94
00006 73
00007 25
00008 86
19, 00009
00010 52
 
# Cat file2
00001 20
00007 28
00002 32
00004 56
00010 52
 
1.2 Implementation commands
# Paste file1 file2 | awk '{if ($1 = $3) & ($2! = $4) {print $0 }}'
00007 31 00007 28
00002 04 00002 32
00010 94 00010 52
Others:
# Cat file1 file2 | sort | uniq-u
 
The answer on the third floor is wrong. what I want is
00002 31 32
00007 25 28
Sort file1> f1; sort file2> f2; join-j1 1 f1 f2 | awk '$2! = $3'
Or
Sort file1> f1; sort file2> f2; join-1 1-2 1 f1 f2 | awk '$2! = $3'
 
 
2. instance 2
# Cat fawk.
1 xiao 25 beijing 9000
2 liuwenjing 24 beijing 5000
3 weijianjun 29 shanghai 8000
4 wanmingyang 28 beijing 5000
5 tianzhiyu 25 beijing 5500
6 zhouhaoxing 23 beijing 5000
 
2.1 shows the full line of xiao, such as $2, or $2, which is equal to liu.
[Root @ store_2 awk] # awk '{if ($2 = "xiao") | ($2 = "liu") print $0} 'fawk. a
1 xiao 25 beijing 9000
 
2.3 display the rows of xiao or $2 including liu, such as $2
[Root @ store_2 awk] # awk '{if ($2 = "xiao") | ($2 ~ /Liu/) print $0} 'fawk.
1 xiao 25 beijing 9000
2 liuwenjing 24 beijing 5000
 
2.3 calculate how many rows of NR are displayed
[Root @ store_2 awk] # awk '{print $0} END {print "total number of rows:" NR} 'fawk.
1 xiao 25 beijing 9000
2 liuwenjing 24 beijing 5000
3 weijianjun 29 shanghai 8000
4 wanmingyang 28 beijing 5000
5 tianzhiyu 25 beijing 5500
6 zhouhaoxing 23 beijing 5000
Total number of rows: 6
2.4 display the last directory name of the current path
Echo $ PWD | awk-F/'{print $ NF }'
Echo $ PWD | awk-F/'{print NF}' # displays the number of columns or fields.
Echo $ PWD | awk-F/'{print NF-1}' # Calculation
2.4 replace functions
# Replacing xiao with hehe
[Root @ store_2 awk] # awk 'gsub (/xiao/, "hehe") 'fawk. a // $0, xiao replaces liu
1 hehe 25 beijing 9000
The same as sed's/xiao/hehe/'fawk. a, but all records are displayed.
2.5 length function
[Root @ store_2 awk] # awk '{print length ($4)}' fawk.
7
7
.....
2.6 split functions
Split (s, a, t) // t is the delimiter, s is the character, a is the array, a [1], a [2]...
 
2.7 calculate the size of all files in the directory
2.7.1 Single command
[Root @ store_2 awk] # ll | awk '{total + = $5} {print $0} END {print total}' # you can add multiple {}
Total 16
-Rw-r -- 1 root 354 10-29 fawk.
-Rwxr-xr-x 1 root 61 10-29 04-34 it. awk
415
 
2.7.2 programming
[Root @ store_2 awk] # cat it. awk
#! /Bin/awk-f
(Total + = $5)
END {print "total size:" total}
[Root @ store_2 awk] # ll |./it. awk
-Rw-r -- 1 root 354 10-29 fawk.
-Rwxr-xr-x 1 root 61 10-29 04-34 it. awk
Total: 415
 
3. instance 3
3.1 Delete all blank rows
Delete all blank lines in a file
Code ::
$ Awk 'NF> 0' test1.dat
One 123-321 234/22
Two 344-637 726/28 c
Three 273-287 287/97 d
Four 872-872 282/20 c
3.2 output an even number of rows of a file
Code ::
$ Awk 'NR % 2 = 0' test1.dat
Two 344-637 726/28 c
Four 872-872 282/20 c

3.3 The input value ranges from 1 to 100.
Code ::
$ Awk 'In in {for (I = 1; I <7; I ++) print int (101 * rand ())}'
24
29
85
15
59
19


3.5 output the number of bytes of all files in the local directory
Code ::
] $ Ls-l | awk '{x + = $5}; END {print x }'
15827
3.6 displays UID> 500 user records
[Root @ store_2 awk] # awk-F: '$3> 500 {print $0}'/etc/passwd
Nfsnobody: x: 65534: 65534: Anonymous NFS User:/var/lib/nfs:/sbin/nologin
You can also write as follows:
[Root @ store_2 awk] # awk-F: '{if ($3> 500) print $0}'/etc/passwd
Nfsnobody: x: 65534: 65534: Anonymous NFS User:/var/lib/nfs:/sbin/nologin
[Root @ store_2 awk] # awk-F: '{if ($3> 500) {print $0}'/etc/passwd
Nfsnobody: x: 65534: 65534: Anonymous NFS User:/var/lib/nfs:/sbin/nologin
 
The following is an incorrect syntax:
A. [root @ store_2 awk] # awk-F: '{$3> 500 print $0}'/etc/passwd
Awk: {$3> 500 print $0}
Awk: ^ syntax error
B. awk-F: '{$3> 500} {print $0}'/etc/passwd
Root: x: 0: 0: root:/bin/bash
Bin: x: 1: 1: bin:/sbin/nologin
Daemon: x: 2: 2: daemon:/sbin/nologin
.........
Author: Fengyun's blog"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.