0

I want to compare two files in which the data is delimited by a pipe |. Both the files have the same results but only few fields might vary.

File 1:

A|B|C|D

File 2:

A|B|F|D

I want to compare two files field by field, that is, by omiting column 3, my result should show zero difference.

I want to find the difference, if any. apart from the few columns that I mention.

closed as unclear what you're asking by jasonwryan, Anthon, Timo, cuonglm, jimmij Jan 30 '15 at 9:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • PLease include sample output and what you have tried yourself... – jasonwryan Jan 30 '15 at 7:19
  • join file1 file2 doesn't do the job? – αғsнιη Jan 30 '15 at 7:25
1

You can use cut to remove unwanted columns. From the man page:

   -d, --delimiter=DELIM
          use DELIM instead of TAB for field delimiter

   -f, --fields=LIST
          select  only  these  fields;   also  print any line that contains 
          no delimiter character, unless the -s option is specified

   --complement
          complement the set of selected bytes, characters or fields

You can use this to create temporary files, which you can compare using diff like that:

cut -d'|' -f 3 --complement <file1 >file1.tmp
cut -d'|' -f 3 --complement <file2 >file2.tmp
diff file1.tmp file2.tmp
  • 1
    You can do without temporary files: diff <(cut -d'|' -f 3 --complement file1) <(cut -d'|' -f 3 --complement file1) – chaos Jan 30 '15 at 7:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.