LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 05-07-2004, 09:49 AM   #1
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Rep: Reputation: 15
Talking Using diff to compare file with common lines, but at different line numbers


Hi,

I wouldn't normally ask this type of question, but I've had a good look on google and can't find the answer, or any decent examples to get me started!

I'm trying to analyse some files containing a few thousand lines of sql commands each. Both of them are used to create a database with the same structure, except one has data in and the other only has the schema. I want to be able to compare them, so that I can output the extra lines of code used to insert the data into the database. From this I hope to replace it with my own data.

I'm aware that I can do this with the diff tool, but can't figure out which options are needed, really I just want an example, can anyone help? This is what I've tried so far:

diff -wbi kernel/setup/packages/blog/sql/mysql/blog.sql kernel/sql/mysql/cleandata.sql > out
and
diff -wbi kernel/sql/mysql/cleandata.sql kernel/setup/packages/blog/sql/mysql/blog.sql > out

neither give me the right results. I think this is something to do with the common lines not necessarily appearing on the same line numbers in each file.

Thanks in advance for any help!

Regards,

James

Last edited by jimieee; 05-07-2004 at 09:51 AM.
 
Old 05-09-2004, 04:03 AM   #2
MasterC
LQ Guru
 
Registered: Mar 2002
Location: Salt Lake City, UT - USA
Distribution: Gentoo ; LFS ; Kubuntu ; CentOS ; Raspbian
Posts: 12,613

Rep: Reputation: 69
Having been a day or 2, I'll suggest using vimdiff instead of diff alone. It's a lot more elegant, and I think will give you the results you are looking for, or at least, closer to it.

Another option:
If the data is simply in a different file, and you want to "truncate" it to the bottom of the other file, you can cat the first file into the second:
cat /home/whatever/file1 >> /home/whatever/file2

Cool
 
Old 05-10-2004, 04:24 AM   #3
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
Hmmm, had a quick go and it seems a lot nicer to user than diff. Need to read some docs to figure it out, but it appears to "know" what I want

Thanks!

~James~
 
Old 05-10-2004, 07:26 AM   #4
jimieee
Member
 
Registered: Aug 2003
Location: London, UK
Distribution: Debian and Fedora for play and RHEL + Solaris for work
Posts: 172

Original Poster
Rep: Reputation: 15
In the end these tools didn't do exactly what I wanted them to do. I started to write a script, which seems to work, but doesn't like the long lines of text (sed doesn't that is) that I ended up dealing with. All this became too timeconsuming and I was starting to lose track of my objectives here, so I'm changing tactics. Here's my script for anyone interested:

Code:
#!/bin/bash
# A quick script to compare lines in two files a just retrieve the ones that
# are different (Delete CLEAN line matches when they appear in DIRTY, but
# output to OUTPUT).
                                                                                                                                                            
CLEAN="cleandata.sql"
DIRTY="blog.sql"
OUTPUT="output.sql"
                                                                                                                                                            
# Get number of lines in CLEAN
CLEAN_LINE_NUM=`wc -l $CLEAN`
# Argh! Get rid of the text that tells you the file you queried!
CLEAN_LINE_NUM=`echo $CLEAN_LINE_NUM | sed "s/$CLEAN//"`
                                                                                                                                                            
# Copy DIRTY TO OUTPUT, because we don't want to overwrite our souces
cat $DIRTY > $OUTPUT
                                                                                                                                                            
# For each line remove check to see if it exists in DIRTY,
# if it does delete it!
                                                                                                                                                            
i=0
temp_input=`cat $DIRTY`
while [ $i -le $CLEAN_LINE_NUM ]
do
    # Find out current line for CLEAN
    line=`sed -n -e "$i"p < $CLEAN`
    # Delete this line from OUPUT if it exists in DIRTY
    sed -e "/$line/d" $temp_input > $OUTPUT
    temp_input=`cat $OUPUT`
    # Increment loop count variable
    echo "`expr $i + 1` lines completed"
    i=`expr $i + 1`
done
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM
Diff compare 2 files DavidTempler Linux - Newbie 2 11-01-2004 10:00 AM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 02:38 AM
How to compare these two strings in one line code? powerplane Programming 4 07-10-2003 12:09 AM
How Compare FS/dirs - can't use 'diff'? MikHud Linux - General 2 05-07-2002 07:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:25 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