LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags ,

Reply
 
Thread Tools Search this Thread
Old 09-09-2008, 03:52 PM   #1
jfarhat
LQ Newbie
 
Registered: Sep 2008
Posts: 1
Thanked: 0
How to get differences between two files using bash script


[Log in to get rid of this advertisement]
Greetings,

I am new to bash scripting and need help.

I have two text files F1 and F2. F1 contains a list of items
1 2 3 a
1 2 3 b
1 2 3 c
1 3 4 d
2 3 4 e
1 3 4 f

The file F2 contains
c
d
e

I need to write a script to show the difference between F1 and F2 where in this case the difference is
a
b
f

Your help is greatly appreciated.

I tried something as follows to store the items 2 and not in 1 but it fails:

exec < $inF1
while read line
do
cat $inF2 | grep -v "echo $line | awk '{ print $4 }" > z.txt
done

Last edited by jfarhat; 09-09-2008 at 03:56 PM.. Reason: missed details
jfarhat is offline  
Tag This Post ,
Reply With Quote
Old 09-09-2008, 04:12 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,495
Thanked: 6
It would be easy in perl, and a lot more efficient that shellscript:
Code:
#!/usr/bin/perl

my %two;
open(TWO, "<F2") || die "cannot open F2 : $!\n";
map { chomp; $two{$_} = 1; } <TWO>;
close(TWO);

open(ONE, "<F1") || die "cannot open F1 : $!\n";
map { chomp; $_ = (split(/\s+/))[3]; print "$_\n" if (!defined($two{$_})); } <ONE>;
close(ONE);
matthewg42 is offline     Reply With Quote
Old 09-09-2008, 04:41 PM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware 12 and current
Posts: 1,212
Thanked: 119
Heres an undoubtedly ugly bit of bash code to do it. Create a script with this as the contents,make it executable and call it with ./script_name file1 file2 as per your example.

Code:
#!/bin/bash
cut -d " " -f4 $1 | sort | sdiff --left-column  - $2 | grep -v "(" | cut -f1
I think there is a much nicer way of doing this but this is my first hack..
bgeddy is online now     Reply With Quote
Old 09-09-2008, 05:53 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 231
Thanked: 18
Moin,

try this:
Code:
TMP_F1=`mktemp` || exit 1 # create a temp file
cut -f4 -d" " file1 | sort >$TMP_F1 # cut the 4th field from file1, sort
comm -3 $TMP_F1 file2 # show all lines not appearing in both files
rm $TMP_F1
if file2 is also not sorted:
Code:
TMP_F1=`mktemp` || exit 1 # create a temp file
TMP_F2=`mktemp` || exit 1 # create a second temp file
cut -f4 -d" " file1 | sort >$TMP_F1 # cut the 4th field from file1, sort
sort file2 >$TMP_F2
comm -3 $TMP_F1 $TMP_F2 # show all lines not appearing in both files
rm $TMP_F1 $TMP_F2
Jan
jan61 is offline     Reply With Quote
Old 09-09-2008, 05:56 PM   #5
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Thanked: 1
Maybe use grep -f ?
Code:
grep -v -f F2 F1 | cut -d' ' -f4
keefaz is offline     Reply With Quote
Old 09-09-2008, 06:05 PM   #6
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Thanked: 126
Not so pretty, but works

Code:
cut -d ' ' -f4 F1 | diff - F2 | grep \< | cut -d ' ' -f2
billymayday is offline     Reply With Quote

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
Bash script that writes to files alpha_hack Programming 3 01-30-2008 06:29 PM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
bash script to copy files thtr2k Programming 1 02-08-2007 01:03 AM
bash script to output only differences in two files czarherr Programming 4 11-08-2004 07:25 PM
bash script to rm all files in a dir keirobyn Programming 8 07-19-2002 08:53 AM


All times are GMT -5. The time now is 05:14 PM.

Main Menu
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.
Advertisement
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Click Here to receive a complimentary subscription courtesy of LQ.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration