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
How to search for keywords in subsequent lines rdhanek Shell Programming and Scripting 12 08-11-2009 05:43 AM
Delete the lines before the first instance of the keyword rdhanek Shell Programming and Scripting 8 07-29-2009 01:07 PM
Delete the lines after the last instance of the keyword rdhanek Shell Programming and Scripting 2 07-29-2009 01:00 PM
replace only 1st word of a line if it comes in the subsequent lines at same postion. geeko Shell Programming and Scripting 9 06-03-2009 07:25 AM
search 2 lines and delete above line shamushamu Shell Programming and Scripting 4 10-06-2008 05:43 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-21-2009
rdhanek rdhanek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 74
search for keyword in subsequent lines and delete the second line

I have my data something like this

I need to search for the keyword yyyy in the susequent lines and if it is present, delete the second line with keyword.

In other words, if a keywords is found in two subsequent lines delete the second line.

input data:
Code:
aaaa bbbbb cccc dddd
xxxx yyyy xxxxx abc
xxxx yyyy xxxxx xyz
aaaa bbbbb cccc dddd
rrrrrrrrr ttttttt yyyy
xxxx yyyy xxxx
aaaa bbbbb cccc dddd
Code:
output expected:
aaaa bbbbb cccc dddd
xxxx yyyy xxxxx abc
aaaa bbbbb cccc dddd
rrrrrrrrr ttttttt yyyy
xxxx yyyy xxxx
aaaa bbbbb cccc dddd
  #2 (permalink)  
Old 08-21-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
shouldn't the output be this?
Code:
aaaa bbbbb cccc dddd
xxxx yyyy xxxxx abc
aaaa bbbbb cccc dddd
rrrrrrrrr ttttttt yyyy
aaaa bbbbb cccc dddd
  #3 (permalink)  
Old 08-21-2009
rdhanek rdhanek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 74
Yes..You are correct!

output expected is

Code:
aaaa bbbbb cccc dddd
xxxx yyyy xxxxx abc
aaaa bbbbb cccc dddd
rrrrrrrrr ttttttt yyyy
aaaa bbbbb cccc dddd
  #4 (permalink)  
Old 08-21-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,300
Should be something like this:

Code:
awk '/yyyy/ && NR!=n {n=NR+1}
/yyyy/ && n==NR {next}
{print}' file
  #5 (permalink)  
Old 08-21-2009
rdhanek rdhanek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 74
That worked Perfect!

thanks
Reply

Bookmarks

Tags
keyword, search, subsequent lines

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 02:22 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