AWK shell command of multiple files

1. first to write a AWK handle multiple files example

The first file to print the first field, the second field of the second file Print

method one:

#!/bin/bash
set -x
file1=$(pwd)"/kk1"
file2=$(pwd)"/kk2"
awk -F' ' '{
        if(FILENAME==ARGV[1]){
           print $1;
        }else{
          print $2;
        }
}' $file1 $file2

Method Two:

#!/bin/bash
set -x
file1=$(pwd)"/kk1"
file2=$(pwd)"/kk2"
:<<EOF
awk -F' ' '{
        if(FILENAME==ARGV[1]){
           print $1;
        }else{
          print $2;
        }
}' $file1 $file2
EOF
awk -F' ' '{ 
        If (NR == FNR) { 
           print $ 1 ; 
        } Else { 
          print $ 2 ; 
        } 
} ' $ File1 $ file2

2.AWK important constants

Command line argument number ARGC 
ARGV command line arguments arranged 
FILENAME awk browse the file name of 

the number of lines FNR browse files (a plurality of files, which is a set value, the new count starts) 
when the number of records read NR (s files , whose value is continuously incremented by one, not set) 
the number of domains NF browsing history (number of rows in each column) 

the FS setting input field separator, which is equivalent to the command line - F. option 
OFS output field separator 

ORS output record separator 
RS control input record separator 
ENVIRON support queue system environment variables using 
$ 0 refers to the entire record variable. $ 1 represents the first field of the current line, $ 2 represents the second field of the current line, ...... and so on. 

$ NF is the number finally, information indicating the last one, with the variable NF is different, variable NF is the total number of statistics for each of the ranks

Specifies the output formatter

 1 #!/bin/bash
 2 set -x
 3 file1=$(pwd)"/kk1"
 4 file2=$(pwd)"/kk2"
 5 
 6 awk -F' 'BEGIN'{
 7             OFS="\t"
 8         }
 9         {
10         if(NR==FNR){
11            print $1;
12         }else{
13           print $ 2 ;
14          }
 15          print NR, FNR, NF;
16 } ' $ file1 file2 $

 

#!/bin/bash

set -x
file1=$1
file2=$2
cat $file1
awk -F ' ' 'BEGIN{
                OFS="\t";
        }
{
        if(FILENAME==ARGV[1]){
            arr[$1]=$1;
        }
        else{
           if($1 in arr){
              gsub(/\$\$/,",",$2);
              len=split($2,brr,",")
              for(i=0;i<=len;i++)
                {
                   print brr[i];
                }
              print $1,$2;      
           }
        }

}END{
    #for(e in arr){
    #   print e;
    #}
}' $file1 $file2

 

ps .awk single quotes content with double quotes

3.AWK built-in common function

 

Guess you like

Origin www.cnblogs.com/sun5/p/11723613.html