1

I have two files named file1 and file2. I want to compare with the NR of first files with the first field of file2 i.e. $1. Like this: if(file1.NR==file2.$1){ then do some calculations}. Please help me if any body can.

File1

t 0.00031
r 0.000558677
r 0.000558884
r 0.000559019
t 0.000649164
r 0.000897651
r 0.000897716
t 0.000987716
............
............

and so on. Total lines i.e. NR in the File1 are 579.

File2

9 0.00031
17 0.000649164
27 0.000987716
37 0.00129372
47 0.00163994
6 0.00194716
66 0.00227338
.............
.............

and so on. The total lines i.e. NR in the File2 are 82.

  • 5
    Please edit your question to include sample input and output... – jasonwryan Feb 6 '15 at 5:52
  • NR is the number of records (lines). Did you intend instead to refer to the number of fields on a line, that is, NF? – John1024 Feb 6 '15 at 5:54
  • I need NR. So can I could use a specific field of it's file if(NR==file2.$1). Thanx – mmkarim91 Feb 6 '15 at 6:05
  • As Jason suggested, you really need to include sample input and the corresponding output that you want. – John1024 Feb 6 '15 at 6:32
  • 1
    All right, here's the deal. Code blocks containing the plaintext files (or just parts of them) mean everyone can test solutions before they recommend one. Screenshots of the files mean people must recreate the files for testing which seems like it'll be a lot of work in this case. Please, use code blocks and plain-text. – HalosGhost Feb 6 '15 at 12:43
1

You are basically trying to use the second file as an index. You could filter the file first, to keep only the wanted lines, and then process the result line-by-line.

It's probably easier to build an awk array of line numbers to process.

awk 'NR==FNR { array[FNR] = $1 }
NR!=FNR { file1_lines[FNR] = $0 }
END { for(linenum in array) { print file1_lines[array[linenum]] } }' file2 file1

You can redirect this to another file and then process the whole of it without filtering.

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.