Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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!
[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
>
[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.
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?:
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?
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
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.