LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-21-2011, 12:19 PM   #1
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Rep: Reputation: 18
delete all lines in a file which consists of particular string


Hi all,

Assume that i am having following lines in a file called test.

subscribe parser for dinesh
extend size for dinesh
subscribe parser for anish
unsubscribe parser for dinesh
extend size for arvind

I want to delete all lines which contains the string "dinesh". Is it possible. If possible kindly post your suggestions.


Thanks in advance,
Dinesh.
 
Old 03-21-2011, 12:20 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,783
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
this will remove any lines matching 'denish'

Code:
grep -Ev 'denish' filename > somenewfile.txt

Last edited by szboardstretcher; 03-21-2011 at 12:23 PM.
 
1 members found this post helpful.
Old 03-21-2011, 12:53 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Code:
sed '/dinesh/d' filename
 
1 members found this post helpful.
Old 03-21-2011, 12:54 PM   #4
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
Hi dinesh.


This code will useful to you

Code:
awk -F " " '{ if( $4!="dinesh" ) print $0 }' filename > somefile
 
1 members found this post helpful.
Old 03-21-2011, 12:58 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,783
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
We've covered "grep, sed, and awk" in one thread!

 
1 members found this post helpful.
Old 03-22-2011, 02:02 AM   #6
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi all,

Thanks all for your suggestions. it works.

when i execute the code like this.

sed '/dinesh/d' samplefile

i get this output.

subscribe parser for anish
extend size for arvind

But i want this output on the same file i.e samplefile. So i tried to redirect the output to samplefile. but it didn't work.

Your suggestions please.
 
Old 03-22-2011, 02:05 AM   #7
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
why leave perl behind :-)

perl -ni -e 'print unless /dinesh/' filename

this will edit the original file, so be careful if you want to save the original file contents.
 
Old 03-22-2011, 02:07 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Please, take a look at man sed and see the -i option.
 
1 members found this post helpful.
Old 03-22-2011, 02:17 AM   #9
dinakumar12
Member
 
Registered: Mar 2010
Location: INDIA (chennai)
Distribution: centos
Posts: 271
Blog Entries: 7

Original Poster
Rep: Reputation: 18
Hi colucix,

Thanks, this code works.

sed -i '/dinesh/d' samplefile
 
  


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
how to copy some lines in a file and delete these lines after gartura Linux - General 1 07-20-2010 09:55 AM
find a string in a file and delete that line as well as x lines after tdnnash25 Linux - Server 3 06-24-2010 03:13 PM
[SOLVED] Delete all lines containing a string, plus 4 lines below it? RedHelix Linux - Newbie 4 01-27-2010 10:13 AM
Delete Duplicate Lines in a file, leaving only the unique lines left xmrkite Linux - Software 6 01-14-2010 07:18 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-21-2007 12:14 AM


All times are GMT -5. The time now is 03:09 PM.

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