The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
remove first few words from a line shellscripter Shell Programming and Scripting 4 10-08-2008 02:34 AM
To read and separate number and words in file and store to two new file using shell kamakshi s Shell Programming and Scripting 2 06-26-2008 08:39 AM
replace words in file based on another file kinmak Shell Programming and Scripting 9 05-07-2008 06:06 AM
Read words from file and create new file using K-shell. bsrajirs Shell Programming and Scripting 4 06-01-2007 01:15 PM
Looking for Words File OC Del Guy UNIX for Dummies Questions & Answers 9 04-12-2006 08:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-19-2008
Beeser Beeser is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 3
Remove words from file

Hello,

I have a question:
I have two different files, let's call them file1 and file2. file1 contains a list of words, the words are on seperate lines:

word1
word2
word3
word4
etc...

file2 also contains a list of words, seperated in the same way as file1.

What I want to do is remove the words that are in both file1 and file2 from file2. Does anyone know if this is possible?

I tried some sed stuff, but I just can't get the desired result.

Many thanks!
  #2 (permalink)  
Old 12-19-2008
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,


Code:
grep -v -f file2 file1

-v -- print only lines not matching pattern
-f file2 -- get the list of possible matches from file2.

HTH Chris

Last edited by vgersh99; 12-19-2008 at 11:23 AM.. Reason: VbCode fixes
  #3 (permalink)  
Old 12-19-2008
SFNYC SFNYC is offline
Registered User
  
 

Join Date: Jun 2008
Location: New York City
Posts: 95

Code:
$ cat file1
word1
word2
word3
word4
word5
word6

$ cat file2
word1
word2
word3
word4
word7
word8
word9

$ comm -13 file1 file2
word7
word8
word9

  #4 (permalink)  
Old 12-19-2008
Beeser Beeser is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 3
Thanks for your replies, but I still dont get the desired result.

I still see the words that occur in file1 in file2 after using these commands.

file1 contains for example words that occur often in a text like:
a
the
I
an
to
be

Those words also occur in file2, but I want to strip them out of file2, so I have a list of words that don't occur that much.

I actually don't see why comm won't work.

Are there other options to solve my problem?
  #5 (permalink)  
Old 12-19-2008
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131

Code:
nawk 'FNR==NR {a[$0];next} !($0 in a)' file1 file2

  #6 (permalink)  
Old 12-19-2008
Beeser Beeser is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 3
Thanks for your help! You just solved my problem
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0