LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 09-09-2008, 02:52 PM   #1
jfarhat
LQ Newbie
 
Registered: Sep 2008
Posts: 1

Rep: Reputation: 0
How to get differences between two files using bash script


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 02:56 PM. Reason: missed details
 
Old 09-09-2008, 03:12 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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);
 
Old 09-09-2008, 03:41 PM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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..
 
Old 09-09-2008, 04:53 PM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
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
 
Old 09-09-2008, 04:56 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe use grep -f ?
Code:
grep -v -f F2 F1 | cut -d' ' -f4
 
1 members found this post helpful.
Old 09-09-2008, 05:05 PM   #6
billymayday
LQ Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Not so pretty, but works

Code:
cut -d ' ' -f4 F1 | diff - F2 | grep \< | cut -d ' ' -f2
 
  


Reply

Tags
file



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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