2

I want to do a line-by-line comparison of values in, say, column 6 of two files, and then echo the lines and their full contents, piping them into a combined file, say the line from one file and then the line from the next file immediately under it, all separated by a spacer line , when the absolute value of the difference in the values in column 6 from the two files is less than some small maximum value, such as < 0.001. I would not echo or print out all the other lines where the absolute value of the difference in the column 6 values is greater than 0.001, for example, but instead, only the cases where it is < 0.001. How would I do that?

  • Some input/output would be much nicer than a large wall of text :) – John WH Smith Dec 5 '14 at 21:51
3
awk '
  function abs(v) {
    return (v < 0 ? -v : v)
  }
  {l1=$0; f6_1=$6; getline < "file2"; l2=$0; f6_2=$6}
  abs(f6_1 - f6_2) < 0.001 {print l1 "\n" l2 "\n"}' file1
| improve this answer | |
1

I would pre-process with paste to interleave the lines and then feed to awk

paste -d '\n' file1 file2 |
awk -v threshold=0.001 'NR % 2 {a=$6; b=$0; next};
$6 - a > -threshold && $6 - a  <threshold {print b; print ""; print $0}'
| improve this answer | |

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.