LinuxQuestions.org
Register a domain and help support LQ
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 09-29-2011, 01:39 AM   #1
deepmala8
LQ Newbie
 
Registered: May 2011
Posts: 14

Rep: Reputation: 0
shell script to delete a pattern from file


Hi, my file is like


tcp:in:d=21:s=192.168.1.53
tcp:in:d=21:s=192.168.1.55
tcp:in:d=21:s=192.168.1.5

and now i want to delete 'tcp:in:d=21:s=192.168.1.5' but by using sed it is replacing all the strings with blank.
I just want to delete the third line and else should be retained.
please help!
 
Old 09-29-2011, 01:44 AM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Hello,

If you are looking to just delete the third line in your file, you could do it by the following:
Code:
sed 3d file.txt
All that does is delete the third line, that is all. If you are looking for another way to do this, let me know. Also a good reference on that is here - http://www.thegeekstuff.com/2009/09/...-and-patterns/

Cheers,

Josh
 
1 members found this post helpful.
Old 09-29-2011, 02:48 AM   #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
Or use word boundaries in the regular expression to match exactly that IP address:
Code:
sed '/\<192.168.1.5\>/d' file
 
1 members found this post helpful.
Old 09-29-2011, 03:34 AM   #4
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by colucix View Post
Or use word boundaries in the regular expression to match exactly that IP address:
Code:
sed '/\<192.168.1.5\>/d' file
That too.... I almost forgot about that. I need to catch back up on my sed knowledge.....
 
Old 09-29-2011, 05:43 AM   #5
16pide
Member
 
Registered: Jan 2010
Posts: 418

Rep: Reputation: 83
Code:
grep -v 192.168.1.5
This can be useful too, especially if you're trying to view a log file in realtime:
Code:
tail -f /var/log/messages|grep -v 192.168.1.5
 
Old 09-29-2011, 11:12 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If you post the actual command(s) you tried to use and the output you got, it would help us to point out just where you were going wrong.
 
Old 09-30-2011, 12:22 AM   #7
deepmala8
LQ Newbie
 
Registered: May 2011
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks to all its done :-)
 
Old 09-30-2011, 12:28 AM   #8
corp769
LQ Guru
 
Registered: Apr 2005
Posts: 5,817

Rep: Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002Reputation: 1002
Quote:
Originally Posted by deepmala8 View Post
Thanks to all its done :-)
Could you share with us what you have used for your script?
 
  


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
Shell Script to Delete line if pattern exists topcat Programming 22 08-23-2011 05:58 AM
[SOLVED] How to search file by pattern and then delete corresponding lines in shell cyatomato Programming 8 09-17-2010 09:08 AM
Shell script to delete file if a pattern is found and restart servers sun81 Linux - Newbie 2 07-22-2010 03:24 PM
Shell Script to Delete part text of a line if pattern matches harsha1980 Programming 36 04-16-2010 04:36 AM


All times are GMT -5. The time now is 08:56 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