LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2013, 01:53 PM   #1
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Rep: Reputation: Disabled
compare and find change in 2 version of file


hii.

I want linux shell script for finding change in 2 version of file line by line.also want to show them.

for example

if old version file is: print("k",k);
new version file is printf("%d",k);

o/p is: add f after print and replace "k" with "%d"
 
Old 04-17-2013, 02:02 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

not sure exactly what you are asking, but have you tried using diff for this?

Evo2.
 
Old 04-17-2013, 02:02 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Can you try:
Code:
~$ awk -F"," '{gsub(/print/,"printf",$1) && gsub(/k/,"\%d,",$1);print $0}' infile.txt
To find diff between 2 files, simply invoke:
Code:
~$ diff file1 file2

Last edited by shivaa; 04-17-2013 at 02:04 PM.
 
Old 04-17-2013, 02:23 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Assuming you want to change print("k",k); into printf("%d",k);, here's a sed solution:
Code:
sed 's|print("k",k);|printf("%d",k);|g' infile > outfile
 
Old 04-17-2013, 02:30 PM   #5
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,

not sure exactly what you are asking, but have you tried using diff for this?

Evo2.

already tried diff command. but is shows o/p like

2a3,4
> komal


but i want o/p in text form like insert, delete particular char....

Last edited by komalp; 04-17-2013 at 02:33 PM.
 
Old 04-17-2013, 02:37 PM   #6
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
Assuming you want to change print("k",k); into printf("%d",k);, here's a sed solution:
Code:
sed 's|print("k",k);|printf("%d",k);|g' infile > outfile
i am making a project in which i need text format of changes in previous version. like some char is deleted or some char is inserted.
 
Old 04-17-2013, 02:59 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
I've read and re-read all your post, but I really don't get what it is you want.

A relevant example (both input files and required output) might clear things up.
 
Old 04-17-2013, 05:00 PM   #8
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by komalp View Post
already tried diff command. but is shows o/p like

2a3,4
> komal


but i want o/p in text form like insert, delete particular char....
diff has options that let you change the way it reports the differences. It sounds a little bit like you want the output to be in the form of instructions on how to convert the old file into the new file. If this is the case, then diff, coupled with patch should do exactly what you want.

Evo2.
 
Old 04-17-2013, 05:37 PM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Try the kompare command if you're using the KDE desktop manager. Similar GUI programs are available for other desktop managers.
 
Old 04-17-2013, 11:49 PM   #10
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
I've read and re-read all your post, but I really don't get what it is you want.

A relevant example (both input files and required output) might clear things up.
here i want to store changes found in new modified file respective to old file in another(o/p) file.

old file:
Quote:
include<stdio.h>
void main()
{
int k;
print("%k",k);
}
new file:

Quote:
#include<stdio.h>
void main()
{
int k;
printf("%d",k);
}
by using diff command we got o/p like
Quote:
1c1
5c5


but i want o/p like
add # at position 1 in line 1
add f at position 13 in line 5
replace"%k" with "%d" at line 5

Last edited by komalp; 04-17-2013 at 11:56 PM.
 
Old 04-17-2013, 11:57 PM   #11
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,


diff has options that let you change the way it reports the differences. It sounds a little bit like you want the output to be in the form of instructions on how to convert the old file into the new file. If this is the case, then diff, coupled with patch should do exactly what you want.

Evo2.
i dnt know how patch work with diff. so can you give me hint??
 
Old 04-18-2013, 08:42 AM   #12
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by komalp View Post
i dnt know how patch work with diff. so can you give me hint??
diff produces a file that shows what lines need to be removed or added. patch can then read that file and can then modify then "old" file to make it identical to the "new" file. This seems to be almost what you want, the main difference being that diff and patch work line by line instead of character by character as you described.
There are a huge number of tutorials on the web. Try searching for "using diff and patch".

Evo2.
 
1 members found this post helpful.
Old 04-19-2013, 02:07 AM   #13
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by evo2 View Post
Hi,


diff produces a file that shows what lines need to be removed or added. patch can then read that file and can then modify then "old" file to make it identical to the "new" file. This seems to be almost what you want, the main difference being that diff and patch work line by line instead of character by character as you described.
There are a huge number of tutorials on the web. Try searching for "using diff and patch".

Evo2.

thank you....for yr help
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compare file extension from two different txt file and find the differences. Neal000 Programming 6 08-28-2012 03:03 PM
How to find version of a file or program rjo98 Linux - Newbie 2 07-14-2009 01:48 PM
using find to compare file mtime with another file's mtime TheFueley Linux - Newbie 1 12-23-2008 09:06 PM
File to find released version of Linux OS ganesh343 Fedora 1 06-06-2006 07:54 AM
[Bash] Find occurences in a file, then change them fr0st Linux - General 2 04-05-2004 10:11 AM


All times are GMT -5. The time now is 08:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration