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?

2

3 Answers 3

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
1
  • 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 4, 2013 at 6: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.

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'

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