DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 24th June 2008
c0mrade's Avatar
c0mrade c0mrade is offline
Port Guard
 
Join Date: May 2008
Posts: 41
Thanked 0 Times in 0 Posts
Default Find pattern in a file and erase it

Hello,

I'd like to find pattern in a file and erase line containing that pattern and 2 lines below that pattern. I know how to find pattern with grep -r pattern and erase line containing that pattern, but can't figure out the rest, how to erase 2 lines below that pattern line. Any suggestions?

Thank you in advance
Reply With Quote
  #2   (View Single Post)  
Old 24th June 2008
lvlamb's Avatar
lvlamb lvlamb is offline
Real Name: Louis V. Lambrecht
Spam Deminer
 
Join Date: May 2008
Location: .be
Posts: 221
Thanked 25 Times in 24 Posts
Default

Quote:
sed -n '/pattern/{N;N;d;}' input.file


In love with one-liners.
__________________
da more I know I know I know nuttin'

Last edited by lvlamb; 24th June 2008 at 09:22 PM.
Reply With Quote
  #3   (View Single Post)  
Old 24th June 2008
c0mrade's Avatar
c0mrade c0mrade is offline
Port Guard
 
Join Date: May 2008
Posts: 41
Thanked 0 Times in 0 Posts
Default

Thank you for your answer : But the file stays unchanged

sed -n '/ptrisn.com/{N;N;d;}' named.conf

I've google something out but still can't get it working
Code:
awk '{if ($0 ~/pattern/) {getline; getline; continue}
            else print }'  old.txt > new.txt
I get this error from awk "awk: (FILENAME=old.txt FNR=126) fatal: `continue' outside a loop is not allowed"
anymore hints ?
Reply With Quote
  #4   (View Single Post)  
Old 24th June 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

sed -i.bak '/ptrisn.com/{N;N;d;}' named.conf

or

perl -i.bak -pe 'if(/ptrisn.com/){<>;<>;$_=undef}' named.conf

btw, lvlamb's sed command is pretty neat.

Last edited by ephemera; 25th June 2008 at 12:19 AM.
Reply With Quote
  #5   (View Single Post)  
Old 25th June 2008
c0mrade's Avatar
c0mrade c0mrade is offline
Port Guard
 
Join Date: May 2008
Posts: 41
Thanked 0 Times in 0 Posts
Default

ephemera you're the king as always you give correct answers to my questions, your the best. tnx
Reply With Quote
  #6   (View Single Post)  
Old 25th June 2008
lvlamb's Avatar
lvlamb lvlamb is offline
Real Name: Louis V. Lambrecht
Spam Deminer
 
Join Date: May 2008
Location: .be
Posts: 221
Thanked 25 Times in 24 Posts
Default

re: file remains unchanged.
Never heard of directing the input to anoter file,
or modify in place?

I deleted 4 letters in my post, unfortunately they seem to be needed.
RTFM
__________________
da more I know I know I know nuttin'
Reply With Quote
  #7   (View Single Post)  
Old 25th June 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

lvlamb, the reason it wasn't working is because of the '-d' option which isn't required in this case.
Reply With Quote
  #8   (View Single Post)  
Old 25th June 2008
lvlamb's Avatar
lvlamb lvlamb is offline
Real Name: Louis V. Lambrecht
Spam Deminer
 
Join Date: May 2008
Location: .be
Posts: 221
Thanked 25 Times in 24 Posts
Default

the -n flag, not -d

Right. I tested with the print function {...;p;} and did not re-re-checked with the delete {...;d
You win

Note that, RTFM should have told why.

Anyway, I learned the use of braces.
__________________
da more I know I know I know nuttin'
Reply With Quote
  #9   (View Single Post)  
Old 25th June 2008
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

Quote:
Originally Posted by lvlamb View Post
You win
Hmm, i can see both of you are making fun of me
Reply With Quote
Reply

Tags
sed

Thread Tools
Display Modes

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP read file contents - Maximum file size cksraj Programming 1 21st September 2009 11:38 AM
K3b cannot find growisofs maxrussell FreeBSD General 5 26th April 2009 12:20 PM
Command to find and replace, but not creating a new file 18Googol2 Programming 4 22nd September 2008 10:28 PM
pkg_add g95;g95 x.f95: cannot find g95 enpey OpenBSD Packages and Ports 8 27th August 2008 12:48 AM
where might I find 'libcamel'? Damien787 FreeBSD Ports and Packages 16 17th June 2008 11:35 PM


All times are GMT. The time now is 03:43 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick