I have two files

First file, namely file1, contains three lines with the following values


Second file, namely frame1 contains following values


Target is to count how many of those values from frame1 file are larger than value which is presented in line1 of file1.

So will be something similar to below however I don't know how to set this as a criterion

awk '($1>??){ ++count } END{ print count }' 'frame1' > 'file-new'
  • 3
    edit your question to include concise, testable sample input (i.e. contents of file1 and file2) and expected output. See How to Ask.
    – Ed Morton
    Nov 26, 2019 at 19:28

2 Answers 2

$ awk 'NR==FNR{ if (FNR==1) tgt=$1; next } $1 > tgt{ ++count } END{ print count+0 }' file1 frame1

With GNU awk you can replace:

if (FNR==1) tgt=$1; next

with the following for efficiency:

tgt=$1; nextfile
  • I am really grateful for your reply. Also in case I have frame2, frame3 etc. and I want to compare their values to the value presented in line 2 and line 3 of file1 respectively how can I modify the above command? Nov 27, 2019 at 11:19
  • ok managed to do this. Instead, I have defined a for do variable,i, and replaced (FNR==1) with (FNR=='$i') Nov 27, 2019 at 11:22
  • Don't do that, it's the wrong approach and using the wrong syntax even for that approach. Ask a followup question.
    – Ed Morton
    Nov 27, 2019 at 17:03

Tried with below command

#Below Command fetches highest value from file1

k=awk 'BEGIN{sum=0}($1 > sum){sum=$1}END{print sum}' file1

#Below command will display value from file2 which is greater  when compared with file1

awk -v k="$k" '$1 > k {print $1}' file2

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.