1

I have two files with some lines being the same.

File A :

command 1{
    some code lines
}
command 2
command 3
command 4{
    some code lines
}

File B :

command 5
command 3
command 1{
    some code lines
}

I'd like to have a final file containing this:

command 1{
    some code lines
}
command 2
command 3
command 4{
    some code lines
}
command 5

Order of lines is not relevant. Is there an existing tool (or a command) to do so?

slm
  • 369,824
merours
  • 193
  • Not a single command that I remember, but you can do it trivially by combining sort and uniq. – Luis Machuca Sep 04 '13 at 06:32
  • Related: http://unix.stackexchange.com/questions/4573/which-gui-diff-viewer-would-you-recommend/4594#4594 – slm Sep 04 '13 at 08:13

3 Answers3

1

I realized this should be a comment answer.

I don't remember if there is a single tool that will do that. But! You can achieve the exact effect by cat'ing the files and then using sort and uniq to do the job:

cat A B | sort | uniq

This throws the contents of both files together, then sorts the lines and removes any duplicates.

Resulting output:

command 1
command 2
command 3
command 4
command 5
  • The trouble is, I can't use sort on files. I updated my example to be more precise. Not sorting makes uniq useless, and sorting makes my file useless :( – merours Sep 04 '13 at 06:45
0

There is no tool to my knowledge that will merge the files in the way that you've shown. Most of the merging/diffing tools work by having some level of context around the various lines so that the tool knows 2 files can go together in various places throughout.

What you're showing doesn't have any real context so I don't see how these tools would be able to do this.

slm
  • 369,824
0

You could do something like:

sed -e 's/_/_u/g;s/%/_p/g;/{$/s/^/%/;/{$/,/}$/b' -e 's/^/%/' A B |
  tr '%\n' '\n%' |
  sort -u |
  tr '%\n' '\n%' |
  sed 's/%//g;s/_p/%/g;s/_u/_/g'

If the files don't contain the % character, you can simplify it to:

sed -e '/{$/s/^/%/;/{$/,/}$/b' -e 's/^/%/' A B |
  tr '%\n' '\n%' |
  sort -u |
  tr -d '\n' |
  tr % '\n'