Skip to main content
added 106 characters in body
Source Link
orion
  • 12.8k
  • 2
  • 33
  • 42

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.

BEGINawk 'NR==FNR { array_len=0array[FNR] = $1 } NR!=FNR { array[array_len++]file1_lines[FNR] = $1$0 } END { for(i=0;i<array_len;i++linenum in array) { do stuffprint withfile1_lines[array[linenum]] array[i]th} line}' offile2 file1 } } 

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

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.

BEGIN { array_len=0 } { array[array_len++] = $1 } END { for(i=0;i<array_len;i++) { do stuff with array[i]th line of file1 } } 

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.

Source Link
orion
  • 12.8k
  • 2
  • 33
  • 42

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.

BEGIN { array_len=0 } { array[array_len++] = $1 } END { for(i=0;i<array_len;i++) { do stuff with array[i]th line of file1 } }