0

I have two files, one with NF=7 and the other has NF=47

I need to check the number of occurrences of $2 in the file with NF=7. If the count is <=2, then I need to make an array from the same file containing arr1[$1]=$1

For this action I used the following code:

awk -F"," '{if(NF==7){arr[$2]++}}END{for(i in a){if(arr[i]<=2){print $0}}}'

For the action of the second file, I need to match $1 from the arr[$1]=$1 taken from the first file with $1 from the second file.

Please find below example:

File1:

1,111,,,,,,
2,111,,,,,,
3,100,,,,,,
4,111,,,,,,

File2:

1,799,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2,899,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
3,999,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Output:

3,999,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
2

You can compare NR with FNR to distinguish between processing the first or the subsequent files. This is because FNR is reset per file, while NR is the running tally. Therefore, only during the processing of the first file will the condition NR==FNR be satisfied.

To process the 'shorter' file, which has to be first...

awk -F, 'NR==FNR{mapper[$1]=$2;counter[$2]++}...'

Then, to process the 'longer' file to make it print when your condition satisfies...

awk -F, 'NR!=FNR&&counter[mapper[$1]]<=2'

Putting both together:

awk -F, 'NR==FNR{mapper[$1]=$2;counter[$2]++}NR!=FNR&&counter[mapper[$1]]<=2'

By default, awk prints the entire line when the selection criteria matches, so simply specifying NR!=FNR&&counter[mapper[$1]]<=2 will be enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.