0

I am running this command in a command prompt

gawk -f comparefirstandsecond.awk First.out Second.out

I want to do it in bash shell script and I am trying it like:

compareAwkOut=$(cat  $diffedRowFromFirstFile | cat $diffedRowFromSecondFile | gawk -f $COMPARE_AWK_SCRIPT)

Where $diffedRowFromFirstFile | cat $diffedRowFromSecondFile are variables holding the file name with full path.

But it does not work, as my Awk script is looking for the actual names of the file to process and it needs to process both files in one go so the array that I create in the processing of the first file exists and I can compare it with contents of the second file.

comparefirstandsecond.awk looks like this:

BEGIN {
  FS=","
}

FILENAME~/^First.out$/ && /^[0-9a-zA-Z]*^^/,/^$/ {
  if ( FILENAME~/Second.out/){
        print "Second.out comes in here as well WHY ? it should not ... "
  }else
    {
        print FILENAME;
        for (i=1;i<=NF;i++) {  
         firstValues[i]=$i
        }
    }
}

FILENAME~/^Second.out$/ && /^[0-9a-zA-Z]*^^/,/^$/ {
  print FILENAME;
  #First.out does not come as it is already processed above
  for (j=1;j<=NF;j++) {
    secondValues[j]=$j
  }

}
END {
  printf "END\n";
  #done something with both arrays for each values like 
  #  for (x in firstValues) {
#    printf("On count %s First  %s and Second %s \n",x,firstValues[x],secondValues[x])
    #if (firstValues[x]!=secondValues[x]) { }
}
}

If I cat them, the file names are lost, I think. It is just a data piped into gawk cat'ed together from both files.

3
  • Please consider describing the problem you're trying to solve rather than the solution. Commented Mar 23, 2017 at 7:43
  • 1
    just pass file names directly, no need to use cat... gawk -f "$COMPARE_AWK_SCRIPT" "$diffedRowFromFirstFile" "$diffedRowFromSecondFile"
    – Sundeep
    Commented Mar 23, 2017 at 7:43
  • 1
    Consider using the NR==FNR idiom - then you don't have to worry about trying to match filenames. Commented Mar 23, 2017 at 7:46

1 Answer 1

0

To capture the output of the gawk command and run the command with filenames stored in the variables, you simply need to do:

compareAwkOut=$(gawk -f $COMPARE_AWK_SCRIPT $filename1 $filename2)

It wasn't clear from your question, but if the filenames are themselves stored in a file named in the variables, this may be what you need:

filename1=$(cat $diffedRowFromFirstFile)
filename2=$(cat $diffedRowFromSecondFile)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .