The UNIX and Linux Forums  


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
sed: deleting 5 lines after a specified pattern pcwiz Shell Programming and Scripting 3 08-14-2009 11:48 AM
counting the lines matching a pattern, in between two pattern, and generate a tab d.chauliac Shell Programming and Scripting 4 03-19-2009 01:30 PM
deleting lines after pattern using sed larne Shell Programming and Scripting 2 10-13-2008 01:54 PM
Pattern Matching and lines after that kaushys Shell Programming and Scripting 4 06-23-2008 12:27 PM
pattern matching over more lines trek Shell Programming and Scripting 3 04-22-2008 07:37 AM

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 4 Weeks Ago
Yorkie99 Yorkie99 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 13
pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as

Uplink traffic:
Downlink traffic:

I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in other words a grep -v won't work)
  #2 (permalink)  
Old 4 Weeks Ago
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 238
put sample of the file and the desired o/p
  #3 (permalink)  
Old 4 Weeks Ago
Yorkie99 Yorkie99 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 13
In cases where successive lines are of the form

Uplink traffic:
Downlink traffic:

I want the "Uplink traffic:" line removed

In cases where successive lines are of the form

Uplink traffic:
Dropped packets: 40
Downlink traffic:

I don't want the "Uplink traffic:" line removed
  #4 (permalink)  
Old 4 Weeks Ago
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Something like this :


Code:
awk '/Uplink traffic:/ {p=NR;st=$0;q=1;next} /Downlink traffic:/ {print (NR==p+1)?$0:$0;q=0;next} { if(q==1){q=0;print st} print}' file_name.txt

  #5 (permalink)  
Old 4 Weeks Ago
Yorkie99 Yorkie99 is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 13
Cheers Panyam.. that worked. No idea how though.. Must have a read of an Awk tutorial and figure it out.
  #6 (permalink)  
Old 4 Weeks Ago
ahmad.diab's Avatar
ahmad.diab ahmad.diab is offline
Registered User
  
 

Join Date: May 2008
Location: Amman Jordan in MEA
Posts: 238
code


Code:
nawk '
/Uplink/{getline a;getline b; if (a !~ /^Down/){printf"%s\n%s\n%s\n\n",$0,a,b} else {printf"%s \n\n",a} }
' up

  #7 (permalink)  
Old 4 Weeks Ago
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Quote:
Originally Posted by ahmad.diab View Post
code


Code:
nawk '
/Uplink/{getline a;getline b; if (a !~ /^Down/){printf"%s\n%s\n%s\n\n",$0,a,b} else {printf"%s \n\n",a} }
' up
Check the script , I guess it wont give the desired result as "yorkie99" expected.
Reply

Bookmarks

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 09:52 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