LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-06-2010, 11:07 AM   #1
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Rep: Reputation: 15
Removing all lines in a file containing sameword.


Friends,

When i want to remove particular lines containing a specific word in from entire document at a time,i am using the following command.

awk '$columnno !~/specificword/' inputfile > outputfile

But here, coulmn no is my problem, because iam having this in different columns. So i need a solution for it.

How to write such removal command without mentioning column no. , ie irrespective of column no, it has to remove all lines having that specific word.

Pl guide me

Rao
 
Old 08-06-2010, 11:12 AM   #2
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,117
Blog Entries: 2

Rep: Reputation: 113Reputation: 113
grep -v word inputfile > outputfile
 
Old 08-06-2010, 11:14 AM   #3
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,200

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
perhaps grep would be a better solution
grep -v pattern file > outputfile
the -v tells grep to list lines that DON'T containt the pattern rather then finding lines that DO
 
Old 08-06-2010, 11:21 AM   #4
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Or you could do it with sed and not need a separate outputfile
Code:
sed -i '/specificword/d' inputfile
 
Old 08-06-2010, 11:31 AM   #5
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,606

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
another sed solution

You could use as already suggested by Andrew
Code:
sed -i '/specificword/ d' inputfile
This will also delete lines that contain 'specificwords'. If this is not desired then use
Code:
sed -e '/\<specificword\>/ d' inputfile > outputfile
 
Old 08-06-2010, 11:36 AM   #6
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Original Poster
Rep: Reputation: 15
Friends,
Thanks it is ok for me.

But when i want to remove many lines , HAVING MULTIPLE WORDS" ie

i want to remove lines having "ABC" and "CDA" and "RMY" etc, which command is to be used.

Pl guide.

Rao
 
Old 08-06-2010, 11:39 AM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
you could use sed's multiple commands' option:
Code:
sed -e '/\<ABC\>/d' -e '/\<BCD\>/d' inputfile > outputfile
Perhaps you can be more specific, if you give us those words, there might be a better way of doing it.

edit: Mind you, the above command will remove the lines containing both and EITHER of the patterns. Is that what you need?

Last edited by sycamorex; 08-06-2010 at 11:40 AM.
 
Old 08-06-2010, 11:54 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Further to sycamorex's solution:
Code:
sed -i -r '/(ABC|CDE)/d' file
 
1 members found this post helpful.
Old 08-06-2010, 11:57 AM   #9
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
Quote:
Originally Posted by grail View Post
Further to sycamorex's solution:
Code:
sed -i -r '/(ABC|CDE)/d' file
As simple as that Why did I think that sed didn't support ERE?
 
Old 08-06-2010, 12:25 PM   #10
secondchanti
LQ Newbie
 
Registered: Jul 2010
Posts: 23

Original Poster
Rep: Reputation: 15
Thanks friends,
My problem is solved
Rao
 
Old 08-06-2010, 01:16 PM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,250

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Quote:
My problem is solved
Then please mark it as SOLVED (see my signature)
 
  


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
removing blank lines in a text file christianunix Linux - Newbie 11 10-29-2007 01:24 AM
Removing new lines from a file psandeepnair1985 Programming 5 03-25-2007 12:46 PM
[bash] removing or editing lines in file Erhnam Programming 12 03-15-2005 08:25 AM
removing lines from file script iluvatar Programming 9 08-20-2004 06:49 AM
Removing lines from file Aylar Programming 2 04-22-2004 07:34 AM


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