LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   adding diff output to the original file (https://www.linuxquestions.org/questions/linux-general-1/adding-diff-output-to-the-original-file-4175435794/)

elliotd123 11-05-2012 04:27 PM

adding diff output to the original file
 
Hi,
I am trying to figure out how I can add the output from a diff (or perhaps some other command) back to the original file on the next line from where it was different.
For example, if file A contains:
a
b
c
d

and file B contains:
b
c
c
d

It would end up as:
a
b
b
c
c
d

The different lines from file B were added after the original line in file A.

Anyone know how I can do this?
Let me know if this isn't clear enough.
Thanks!

pixellany 11-05-2012 06:17 PM

I created two files---"dif" and "dif1"
Code:

[mherring@herring_desk play]$ more dif
one
three
five
[mherring@herring_desk play]$ more dif1
two
four
six

[mherring@herring_desk play]$ diff dif dif1 >> dif
[mherring@herring_desk play]$ more dif
one
three
five
1,3c1,4
< one
< three
< five
---
> two
> four
> six
>


elliotd123 11-05-2012 09:58 PM

Quote:

Originally Posted by pixellany (Post 4823016)
I created two files---"dif" and "dif1"
Code:

[mherring@herring_desk play]$ more dif
one
three
five
[mherring@herring_desk play]$ more dif1
two
four
six

[mherring@herring_desk play]$ diff dif dif1 >> dif
[mherring@herring_desk play]$ more dif
one
three
five
1,3c1,4
< one
< three
< five
---
> two
> four
> six
>


Close, but what I want to do, in this situation, would get an output of:
one
two
three
four
five
six

In other words, it would take the different line, and place it after the original line.

pixellany 11-06-2012 06:50 AM

Sorry--I missed that detail. It seems you are looking for something that "synchronizes" one file to another. My knowledge of these is essentially zero----have you looked at something like rsync?

In your modification of my simple example, the data simply gets interleaved---would that always be the rule? eg, what would be the result in this case?:

file 1:
one
three
four
five
six

file 2:
two
four
five
six
seven

elliotd123 11-06-2012 08:39 AM

Quote:

Originally Posted by pixellany (Post 4823319)
Sorry--I missed that detail. It seems you are looking for something that "synchronizes" one file to another. My knowledge of these is essentially zero----have you looked at something like rsync?

In your modification of my simple example, the data simply gets interleaved---would that always be the rule? eg, what would be the result in this case?:

file 1:
one
three
four
five
six

file 2:
two
four
five
six
seven

More of a synchronozation thing, I will look into rsync...interleaving wouldn't work because if there are two identical lines, I would want it to ignore it. Maybe this is a better way to describe it:
I want it to do essentially the same thing as a diff command, but instead of outputting just the different lines, I want it to add those different lines back to the original file, after the line that they differ from. Make sense?

allend 11-07-2012 07:23 AM

If your files have equal numbers of lines then perhaps this meets your requirements.
Code:

bash-4.2$ cat fileA
one
three
five

bash-4.2$ cat fileB
two
four
six

bash-4.2$ diff -y fileA fileB | awk '{print $1; if ($3) print$3}'
one
two
three
four
five
six

bash-4.2$ cat file1
cat
dog
ram
bat

bash-4.2$ cat file2
cat
dog
ewe
bat

bash-4.2$ diff -y file1 file2 | awk '{print $1; if ($3) print$3}'
cat
dog
ram
ewe
bat


elliotd123 11-07-2012 08:44 AM

Quote:

Originally Posted by allend (Post 4824086)
If your files have equal numbers of lines then perhaps this meets your requirements.
Code:

bash-4.2$ cat fileA
one
three
five

bash-4.2$ cat fileB
two
four
six

bash-4.2$ diff -y fileA fileB | awk '{print $1; if ($3) print$3}'
one
two
three
four
five
six

bash-4.2$ cat file1
cat
dog
ram
bat

bash-4.2$ cat file2
cat
dog
ewe
bat

bash-4.2$ diff -y file1 file2 | awk '{print $1; if ($3) print$3}'
cat
dog
ram
ewe
bat


Thanks, this is exactly what I was looking for, but it won't work for what I need it for, due to some issues I didn't realize in the beginning.
Thanks again!


All times are GMT -5. The time now is 09:22 AM.