-2

File1:

judi /export/home 76
judi /usr 83

File2:

judi /export/home 79
judi /usr 82

if COLUMN3 of File2 is greater that COLUMN3 of File1, the command should print File2's line; for example, for the files above, the command should print:

judi /export/home 79 

And not:

judi /usr 82
4
  • 1
    Are you interested in knowing how to compare the two files using Ubuntu or on how to write a program to do this?
    – itsols
    Nov 9, 2015 at 15:58
  • I am interested to write a program to do this, may be a awk one liner
    – judi
    Nov 9, 2015 at 16:01
  • In your files are only two columns
    – A.B.
    Nov 9, 2015 at 16:02
  • three columns @A.B
    – judi
    Nov 9, 2015 at 16:03

1 Answer 1

0

Using awk:

awk 'NR==FNR{x[NR]=$3}{if($3>x[FNR]){print}}' File1 File2
  • NR==FNR{x[NR]=$3}: if the current record number is equal to the current file record number (i.e. we're processing the first file), assigns the value of the third field to x[NR];
  • {if($3>x[FNR]){print}}: if the third field is greater than x[FNR], prints the record.
% cat File1
judi /export/home 76
judi /usr 83
% cat File2
judi /export/home 79
judi /usr 82
% awk 'NR==FNR{x[NR]=$3}{if($3>x[FNR]){print}}' File1 File2
judi /export/home 79
4
  • Thanks very much @kos - : awk 'NR==FNR{a[$1$2]=$3;next}$3>a[$1$2]&&a[$1$2]{print $0}' file1 file2 this one liner helps
    – judi
    Nov 9, 2015 at 16:28
  • @judi I missunderstood the question. Updating it, but what should happen if there are only 2 columns (for example like in the your example files' last lines)?
    – kos
    Nov 9, 2015 at 16:33
  • Just a small background, I have a defined threshold values for the system mount points like / /opt /usr /var in File1. I am exporting the current usage values to File2, I am using both file format as same, both file will have 3 columns, the rows may vary based on the mount points
    – judi
    Nov 9, 2015 at 16:37
  • @judi I confused the prompt after the files for lines of the files themselves, check the updated answer
    – kos
    Nov 9, 2015 at 16:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .