LinuxQuestions.org
Visit Jeremy's Blog.
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 08-15-2009, 02:31 PM   #1
xpto09
LQ Newbie
 
Registered: Oct 2007
Posts: 18

Rep: Reputation: 0
Compare two files using sed/grep


I want to compare the files "a" and "b" and put in the file "f" only the strings that are in "b" and
not are in "a"

The files "a" and "b":

a:
word1
word2
word3
word4
word5

b:
word1
word2
word7
word8


diff a b > c
--- a Sat Aug 15 15:41:19 2009
+++ b Sat Aug 15 15:41:47 2009
@@ -1,6 +1,4 @@
word1
word2
-word3
-word4
-word5
-
+word7
+word8

grep -i + c > d
+++ b Sat Aug 15 15:41:47 2009
@@ -1,6 +1,4 @@
+word7
+word8

sed 's .\{1\} ' d > e
++ b Sat Aug 15 15:41:47 2009
@ -1,6 +1,4 @@
word7
word8

sed 1,2d e > f
word7
word8

What can I do to "reduce" the commands?
 
Old 08-15-2009, 03:07 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can try the command comm:
Code:
comm -1 -3 a b
but it works for sorted files only.
 
Old 08-15-2009, 03:12 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,508

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
To answer to your question, first you can use pipes instead of saving the result in temporary files:
Code:
diff a b | sed -n '/^+\b/p' | sed 's/+//g'
The sequence \b is for "word boundary". I used it to print only lines with one + sign at the beginning of the line.
 
Old 08-15-2009, 03:52 PM   #4
xpto09
LQ Newbie
 
Registered: Oct 2007
Posts: 18

Original Poster
Rep: Reputation: 0
Code:
diff a b | sed -n '/^+\b/p' | sed 's/+//g'
Solved!
 
Old 09-23-2011, 10:20 AM   #5
gilles.rochefort
LQ Newbie
 
Registered: Sep 2011
Posts: 1

Rep: Reputation: Disabled
You can try with grep

Code:
$ grep -Fxvf a b
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Grep or Sed ? How can I use it ? ArOnaXx Linux - Newbie 6 04-12-2009 02:08 PM
grep -B using sed w3bd3vil Programming 3 08-11-2008 06:37 PM
how to use grep/sed tnik Linux - General 3 11-06-2007 07:51 PM
I need some help with SED and/or GREP... TheEngineer Linux - Newbie 1 02-02-2007 08:17 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM


All times are GMT -5. The time now is 03:00 AM.

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.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration