The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
find pattern delete line with pattern and line above and line below nickg UNIX for Dummies Questions & Answers 1 01-28-2009 06:46 PM
sed find matching pattern delete next line aveitas Shell Programming and Scripting 2 12-22-2008 01:54 PM
comment/delete a particular pattern starting from second line of the matching pattern imas Shell Programming and Scripting 4 10-13-2008 02:37 AM
Urgent Help required : awk/sed help to find pattern and delete till end of line rajan_san UNIX for Advanced & Expert Users 1 08-25-2008 05:18 PM
Urgent! Sed/Awk Filter Find Pattern Delete Till End Of Line rajan_san Shell Programming and Scripting 2 08-25-2008 04:53 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-28-2009
nickg nickg is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 23
find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this:


FRM CHK 0000



I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below.
  #2 (permalink)  
Old 01-28-2009
pmm pmm is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 50
nawk ' /FRM CHK 0000/ { getline;getline;RECORD="" } !/FRM CHK 0000/ { if ( RECORD != "" ) print RECORD; RECORD=$0 } END { if ( RECORD != "" ) print RECORD } ' file
  #3 (permalink)  
Old 01-29-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361

Code:
awk '
/FRM CHK 0000/ { print last; print; x = 1; next }
        x == 1 { print; x = 0 }
               { last = $0 }
'  "$FILE"
  #4 (permalink)  
Old 01-29-2009
nickg nickg is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 23
pmm - we don't have nawk on our system. Thanks though.

cfajohnson- your script prints the lines I am looking for. I am looking to delete those lines (and print everything except those lines).
  #5 (permalink)  
Old 01-29-2009
nickg nickg is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 23
I got this from phv and it works good.

awk '
/FRM CHK 000000/{x="";getline;next}
{if(x)print x;x=$0}
END{if(x)print x}
' filename
Closed Thread

Bookmarks

Tags
sed delete line

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0