Awk processes multiple files

Source: Internet
Author: User

 

Awk processes multiple files

Awk processes multiple files one by one.

Demo1

$ cat $ cat 1.txt a 1 b 2c 3d 4$ cat 2.txt b 5 c 6d 7e 8$ awk ‘{print $0}‘ 1.txt 2.txta 1 b 2c 3d 4b 5 c 6d 7e 8

So how can we tell which file is being processed? Let's take a look at the built-in variables of awk.

Filename: Name of the file currently entered
FNR: Number of records of the current input file
NR: Total number of records that awk starts to process
NF: Number of fields in the current record
FS: Input field delimiter
OFS: Output field separator
RS: Input record Separator
ORS: Output record Separator
Argind: Indicates the current processing parameter. The value of 1.txt in demo1 is 1 and 2.txt is 2.
Argv: Command Line Parameter array. For demo1, argv [1] = "1.txt" argv [2] =" 2.txt"
Argc: Number of command line parameters

We can handle the following two files:

Method 1 -- by FNR & NR
It can be seen that FNR is equal to Nr only when the first file is processed, so it can be as follows:

$ awk ‘FNR==NR{print $0}FNR!=NR{print $0}‘ 1.txt 2.txt $ awk ‘FNR==NR{print $0}FNR<NR{print $0}‘ 1.txt 2.txt

Method 2 -- by filename

$ awk ‘FILENAME=="1.txt"{print "file1:"$1}FILENAME=="2.txt"{print "file2:" $2}‘ 1.txt 2.txt

Method 3 -- by filename & argv
Method 2 is not very flexible, because the entire command line needs to be changed the next time you change the input. In fact, you can use argv to obtain the input variable name, awk stores all input variables in the array argv. Therefore, you can obtain the input file name based on argv to improve the flexibility of the command line.

$ awk ‘FILENAME==ARGV[1]{print "file1:"$1}FILENAME==ARGV[2]{print "file2:" $2}‘ 1.txt 2.txt

Method 4 -- by argind
Method 3 seems to be a little complicated and can be implemented through argind.

$ awk ‘ARGIND==1{print "file1:"$1}ARGIND==2{print "file2:" $2}‘ 1.txt 2.txt

The above four running results are:

file1:a 1 file1:b 2file1:c 3file1:d 4file2:b 5 file2:c 6file2:d 7file2:e 8

Method 1 does not work when processing more than two files. You can use methods 2, 3, and 4.

Example:

Demo2:Compare the first column of the two files with the same row, and put the same row in file 1printOutput. For file 1 and file 2 in demo1, the final output is as follows:

b 2c 3d 4

Implementation Method:

$ awk ‘FNR==NR{a[$1]=$0}FNR!=NR{print a[$1]}‘ 1.txt 2.txt

First, we use 1.txtas a dictionary. The keyis the first column, and The valueis the first line. Then, we can take the first column of 2.txt as the key and obtain the value in the dictionary.

Demo3:Compare the 1-4 characters of file1 with the 2-5 Characters of file2, if the same, merge the second column of file2 with file1 file3 (http://bbs.chinaunix.net/thread-577044-1-1.html)

$ cat file10011AAA 200.00 20050321 0012BBB 300.00 20050621 0013DDD 400.00 20050622 0014FFF 500.00 20050401 $ cat file2I0011  11111 I0012  22222 I0014  55555 I0013  66666 $ awk ‘NR==FNR{a[substr($1,1,4)]=$0}NR!=FNR&&a[b=substr($1,2,5)]{print a[b] $2}‘ file1 file20011AAA 200.00 20050321 111110012BBB 300.00 20050621 222220014FFF 500.00 20050401 555550013DDD 400.00 20050622 66666

Note the following:&&a[b=substr($1,2,5)]This is equivalent to a precondition, that is, if the 2-5 character of file2 does not appear, the print statement following it will not be executed.

Demo4:Enter the following two files:

$ cat 1.txt 10/05766798607,11/20050325191329,29/0.1,14/05766798607 10/05767158557,11/20050325191329,29/0.08,14/05767158557 $ cat 2.txt 05766798607 05766798608 05766798609 

Output:

10/05766798607,11/20050325191329,29/0.1,14/05766798607 

Implementation Method:

$ awk -F‘[/,]‘ ‘NR==FNR{a[$0]=$0}FNR!=NR{if ($2 in a) print $0}‘ 2.txt 1.txt$ awk ‘BEGIN{FS="[/,]"}NR==FNR{a[$0]}NR!=FNR{if ($2 in a) print $0}‘ 2.txt 1.txt 

The results of the first and second executions are the same,-FAndFSThe results are the same, and they are all set as input delimiters.

 

Awk processes multiple files

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.