LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 11-05-2012, 04:27 PM   #1
elliotd123
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
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!
 
Old 11-05-2012, 06:17 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
>
 
Old 11-05-2012, 09:58 PM   #3
elliotd123
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
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.
 
Old 11-06-2012, 06:50 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 11-06-2012, 08:39 AM   #5
elliotd123
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pixellany View Post
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?
 
Old 11-07-2012, 07:23 AM   #6
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,374

Rep: Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754
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
 
1 members found this post helpful.
Old 11-07-2012, 08:44 AM   #7
elliotd123
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by allend View Post
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!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding output of one command to the bottom of another file? romeo0307 Linux - Newbie 4 05-08-2012 02:09 PM
Mencoder adds the extension of the original file to the output file ex: file.flv.mp4 linuxlicious Linux - General 2 04-17-2012 02:22 PM
Mencoder adds the extension of the original file to the output file ex: file.flv.mp4 linuxlicious Linux - General 1 04-15-2012 04:07 AM
[bash] auto fix file from diff output RaptorX Programming 3 08-02-2009 05:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration