LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-28-2009, 06:18 AM   #1
asiandude
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Rep: Reputation: 0
Smile Remove lines in a text file based on another text file


I have a text file called file1.txt containing many lines

eg.

line1
line2
line3
line4
line5
line6


Then i have another text file called file2.txt contains

3
5
6


Is there a command to remove the lines in file1.txt based on the keywords in file2.txt? note: It should remove line3,line5,line6 based on 3,5,6




Thanks.
 
Old 01-28-2009, 06:55 AM   #2
burschik
Member
 
Registered: Jul 2008
Posts: 159

Rep: Reputation: 31
This question is asked so often it sounds like homework.
 
Old 01-28-2009, 07:31 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
sed -ni $(xargs -I: echo -n :d\;<file2.txt;echo p) file1.txt

Last edited by Hko; 01-28-2009 at 07:33 AM.
 
Old 01-28-2009, 07:50 AM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Or grep -vf file2.txt file1.txt might be a solution. (See info grep for details.)

Echoing burschik's question, is this, perhaps, a "qualifying question" for a job or certification? Please tell us why you're asking this question?
 
Old 01-28-2009, 10:33 AM   #5
asiandude
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks guys.. it works for my examples
however it failed to run when file2.txt contains some full keywords.
 
Old 01-28-2009, 11:28 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by asiandude View Post
however it failed to run when file2.txt contains some full keywords.
So please, post a REAL example.
 
Old 01-28-2009, 12:06 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Quote:
Originally Posted by asiandude View Post
Thanks guys.. it works for my examples
however it failed to run when file2.txt contains some full keywords.
Yes, please explain what you mean by "full keywords."
Code:
$ #Sample data from first post

$ cat file1.txt
line1
line2
line3
line4
line5
line6

$ cat file2.txt
3
5
6

$ # Output from proposed solution

$ grep -vf file2.txt file1.txt
line1
line2
line4

$ # Longer test strings.  Is "line3" a "full keyword?"

$ cat file3.txt
line3
ne5
6

$ Output from proposed solution

$ grep -vf file3.txt file1.txt
line1
line2
line4
 
Old 01-28-2009, 09:08 PM   #8
asiandude
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
I just checked my remove-words list and found a . in it. I want to remove lines that contain a '.' as well.
 
Old 01-28-2009, 10:42 PM   #9
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,154

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
When you read info grep, you should have noted the the contents of the file referenced by the -f argument are regular expressions, not simple strings. Thus "." is the wild character.

So, obviously, you need to "escape" the period before you can use it:
Code:
$ cat file1.txt 
line1
line2
line3
line4
line5
line6
Line.1
Line.7

$ cat file2.txt 
3
5
6
\.

$ grep -vf file2.txt file1.txt
line1
line2
line4
 
Old 01-29-2009, 01:45 AM   #10
asiandude
LQ Newbie
 
Registered: Dec 2008
Posts: 6

Original Poster
Rep: Reputation: 0
Thanks PTrenholme. You have been very helpful. I will go read up more.
 
Old 01-29-2009, 11:59 AM   #11
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

If you don't need (or want) the regular expression processing, consider fgrep (or grep -F in some versions), which does not treat characters as having the magic of regular expressions ... cheers, makyo
 
  


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
Adding lines of text to beginning of a text file BillKat Programming 2 01-19-2009 11:40 AM
text match pipe to file then delete from original text file create new dir automatic tr1px Linux - Newbie 6 09-10-2008 10:40 PM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 03:36 PM
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 11:55 AM
Remove odd lines from a text file Mr. Gone Programming 2 09-19-2005 12:16 PM


All times are GMT -5. The time now is 01:07 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration