2

Since I am learning awk; I found out FNR==NR approach is a very common method to process two files. If FNR==NR; then it is the first file, when FNR reset to 1 while reading every line from concatenated files it means !(FNR==NR) and it is obviously the second file.

When it comes to three or more files I can't see a way which is second and third file as both have the same !(FNR==NR) condition. This made me to try to figure out how can there be something like FNR2 and FNR3?

So I implemented a method to process three files in one awk. Assuming like there is FNR1 FNR2 FNR3 for each file. For every file I made for loop that runs seperately. Condition is same for every loop NR==FNR# and actually get what I expected:

So I wonder if there are more sober, concise methods that deliver similar results with belowawkcode

Sample File Contents

$ cat file1
X|A1|Z
X|A2|Z
X|A3|Z
X|A4|Z
$ cat file2
X|Y|A3
X|Y|A4
X|Y|A5
$ cat file3
A1|Y|Z
A4|Y|Z

AWK for loop

    $ cat fnrarray.sh 
awk -v FS='[|]' '{ for(i=FNR ; i<=NR && i<=FNR && NR==FNR; i++)         {x++; print "NR:",NR,"FNR1:",i,"FNR:",FNR,"\tfirst file\t"}
                   for(i=FNR ; i+x<=NR && i<=FNR && NR==FNR+x; i++)     {y++; print "NR:",NR,"FNR2:",i+x,"FNR:",FNR,"\tsecond file\t"}
                   for(i=FNR ; i+x+y<=NR && i<=FNR && NR==FNR+x+y; i++) {print "NR:",NR,"FNR3:",i+x+y,"FNR:",FNR,"\tthird file\t"}
}' file1 file2 file3 

Current and desired output

$ sh fnrarray.sh
NR: 1 FNR1: 1 FNR: 1    first file  
NR: 2 FNR1: 2 FNR: 2    first file  
NR: 3 FNR1: 3 FNR: 3    first file  
NR: 4 FNR1: 4 FNR: 4    first file  
NR: 5 FNR2: 5 FNR: 1    second file 
NR: 6 FNR2: 6 FNR: 2    second file 
NR: 7 FNR2: 7 FNR: 3    second file 
NR: 8 FNR3: 8 FNR: 1    third file  
NR: 9 FNR3: 9 FNR: 2    third file

You can see NR is aligning with FNR# and it is readable which NR is for which file#.


Another Method

I found this method FNR==1{++f} f==1 {} here Handling 3 Files using awk

But this method is replacing arr1[1] when new line is read every time

Fail attempt 1

$ awk -v FS='[|]' 'FNR==1{++f} f==1 {split($2,arr); print arr1[1]}' file1 file2 file3 
A1
A2
A3
A4

Success with for loop (arr1[1] is not changed)

$ awk -v FS='[|]' '{for(i=FNR ; i<=NR && i<=FNR && NR==FNR; i++) {arr1[++k]=$2; print arr1[1]}}' file1 file2 file3 
A1
A1
A1
A1

  • Do you want to do different conditions check for different files, while reading more than 3 files? – RavinderSingh13 Jul 08 '21 at 12:09
  • yes @RavinderSingh13, i will create `arr1[i]=$2` from `file1`, `arr2[j]=$3` from `file2`, `arr3[k]=$1` from `file3`. then create `arr12` for the elements which are common in `arr1` and `arr2` and `arr123` for common elements present in `arr1 arr2 arr3` – Ahmet Said Akbulut Jul 08 '21 at 13:43

2 Answers2

2

When it comes to three or more files I can't see a way which is second and third file as both have the same !(FNR==NR) condition. This made me to try to figure out how can there be something like FNR2 and FNR3?

Here is example:

$ cat f1
X|A1|Z
X|A2|Z
X|A3|Z
X|A4|Z

$ cat f2
X|Y|A3
X|Y|A4
X|Y|A5

$ cat f3
A1|Y|Z
A4|Y|Z

Sample output:

$ awk -F '|' 'FNR==1{file++}{array[file, FNR]=$0; max=max>FNR?max:FNR}END{for(f=1; f<=file; f++){ for(row=1; row<=max; row++){ key=f SUBSEP row; if(key in array)print "file: "f,"row :"row,"record: "array[key]   } }}' f1 f2 f3
file: 1 row :1 record: X|A1|Z
file: 1 row :2 record: X|A2|Z
file: 1 row :3 record: X|A3|Z
file: 1 row :4 record: X|A4|Z
file: 2 row :1 record: X|Y|A3
file: 2 row :2 record: X|Y|A4
file: 2 row :3 record: X|Y|A5
file: 3 row :1 record: A1|Y|Z
file: 3 row :2 record: A4|Y|Z

Explanation:

awk -F '|' 'FNR==1{                   # FNR will reset for every file
              file++                  # so whenever FNR==1 increment variable file
            }
            {
              # array name      : array
              # array key being : file, FNR
              # array value     : $0 which current record/row
              array[file, FNR] = $0; 
              # here we find which row count in all available files
              max = max > FNR ? max : FNR
            }

            END{                     # end block when all files are read
             # start iterating over file
             # as we now variable file hold total no files read
             for(f=1; f<=file; f++)
             { 
                  # iterate now for record from each file
                  # variable max holds max row count
                  for(row=1; row<=max; row++)
                  { 
                      # variable key will now have
                      # key = file-number SUBSET row-number
                      key=f SUBSEP row; 
                      # if key exists in array 
                      # print array value
                      if(key in array)
                           print "file: "f,"row :"row,"record: "array[key] 
                  } 
             }
            }' f1 f2 f3

Other option would be to use true multi-dimensional arrays like below. gawk specific of course.

Assuming filenames are unique, otherwise use FNR==1{ file++} and in place of FILENAME use file

$ awk --version
GNU Awk 4.2.1, API: 2.0 (GNU MPFR 3.1.6-p2, GNU MP 6.1.2)
Copyright (C) 1989, 1991-2018 Free Software Foundation.

$ awk -F '|' '{
              true_multi_array[FILENAME][FNR] = $0
            }
            END{
              for(file in true_multi_array)
                for(row in true_multi_array[file]) 
                  print  "file:",file, "row :" row, "record:" true_multi_array[file][row] 
            }' f1 f2 f3
file: f1 row :1 record:X|A1|Z
file: f1 row :2 record:X|A2|Z
file: f1 row :3 record:X|A3|Z
file: f1 row :4 record:X|A4|Z
file: f2 row :1 record:X|Y|A3
file: f2 row :2 record:X|Y|A4
file: f2 row :3 record:X|Y|A5
file: f3 row :1 record:A1|Y|Z
file: f3 row :2 record:A4|Y|Z
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
1

To identify files in order using GNU awk no matter what:

awk '
    ARGIND == 1 { do 1st file stuff }
    ARGIND == 2 { do 2nd file stuff }
    ARGIND == 3 { do 3rd file stuff }
' file1 file2 file3

e.g. to get the text under "output" in your question from the 3 sample input files you provided:

awk '
    ARGIND == 1 { pos = "first" }
    ARGIND == 2 { pos = "second" }
    ARGIND == 3 { pos = "third" }
    { print "NR:", NR, "FNR" ARGIND ":", NR, "FNR:", FNR, pos " file" }
' file1 file2 file3
NR: 1 FNR1: 1 FNR: 1 first file
NR: 2 FNR1: 2 FNR: 2 first file
NR: 3 FNR1: 3 FNR: 3 first file
NR: 4 FNR1: 4 FNR: 4 first file
NR: 5 FNR2: 5 FNR: 1 second file
NR: 6 FNR2: 6 FNR: 2 second file
NR: 7 FNR2: 7 FNR: 3 second file
NR: 8 FNR3: 8 FNR: 1 third file
NR: 9 FNR3: 9 FNR: 2 third file

or using any awk if all file names are unique whether any of them are empty or not:

awk '
    FILENAME == ARGV[1] { do 1st file stuff }
    FILENAME == ARGV[2] { do 2nd file stuff }
    FILENAME == ARGV[3] { do 3rd file stuff }
' file1 file2 file3

or if the files aren't empty then whether unique or not (note file1 twice in the arg list):

awk '
    FNR == 1 { argind++ }
    argind == 1 { do 1st file stuff }
    argind == 2 { do 2nd file stuff }
    argind == 3 { do 3rd file stuff }
' file1 file2 file1

if a file names can appear multiple times in the arg list and some of the files could be empty then it becomes trickier with a non-GNU awk which is why GNU awk has ARGIND, e.g. something like (untested):

awk '
    BEGIN {
        for (i=1; i<ARGC; i++) {
            fname = ARGV[i]
            if ( (getline line < fname) > 0 ) {
                # file is not empty so save its position in the args
                # list in an array indexed by its name and the number
                # of times that name has been seen so far
                arginds[fname,++tmpcnt[fname]] = i
            }
            close(fname)
        }
    }
    FNR == 1 { argind = arginds[FILENAME,++cnt[FILENAME]] }
    argind == 1 { do 1st file stuff }
    argind == 2 { do 2nd file stuff }
    argind == 3 { do 3rd file stuff }
' file1 file2 file1
Ed Morton
  • 188,023
  • 17
  • 78
  • 185