awk : Filter a set of data to parse header line and last field of multiple same match. | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

awk : Filter a set of data to parse header line and last field of multiple same match.

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-30-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 26 August 2014, 3:05 PM EDT
Posts: 224
Thanks: 143
Thanked 13 Times in 12 Posts
awk : Filter a set of data to parse header line and last field of multiple same match.

Hi Experts,

I have a data with multiple entry , I want to filter PKG= & the last column "00060110" or "00088150" in the output


file:

Code:
###############################################################################################
PKG= P8SDB     :: VGS = vgP8SOra vgP8SDB1 vgP8S001 vgP8S007 vgP8S003 vgP8S004 vgP8SOlog 
PKG= P8SDB    disks:                    
-----------------------------------------------------------------------------------------------
/dev/rdisk/disk7777   xx  00   00  xxxx  07:67  xxxx 00060110
/dev/rdisk/disk7737   xx  00   00  xxxx  02:03  xxxx 00060110
/dev/rdisk/disk7741   xx  00   00  xxxx  07:65  xxxx 00060110
/dev/rdisk/disk7744   xx  00   00  xxxx  07:68  xxxx 00060110
/dev/rdisk/disk7745   xx  00   00  xxxx  07:66  xxxx 00060110
/dev/rdisk/disk7746   xx  00   00  xxxx  07:64  xxxx 00060110
/dev/rdisk/disk7750   xx  00   00  xxxx  07:63  xxxx 00060110
/dev/rdisk/disk7751   xx  00   00  xxxx  02:00  xxxx 00060110
/dev/rdisk/disk7753   xx  00   00  xxxx  02:07  xxxx 00060110
/dev/rdisk/disk7754   xx  00   00  xxxx  07:67  xxxx 00060110
/dev/rdisk/disk7755   xx  00   00  xxxx  02:01  xxxx 00060110
----------------------------------------------------------------------------------------------
###############################################################################################
PKG= P9SDB     :: VGS = vgP9SOra vgP9SDB1 vgP9S001 vgP9S007 vgP9S003 vgP9S004 vgP9SOlog 
PKG= P9SDB    disks:                    
-----------------------------------------------------------------------------------------------
/dev/rdisk/disk7778   xx  00   00  xxxx  06:67  xxxx 00088150
/dev/rdisk/disk7739   xx  00   00  xxxx  03:03  xxxx 00088150
/dev/rdisk/disk7742   xx  00   00  xxxx  06:65  xxxx 00088150
/dev/rdisk/disk7743   xx  00   00  xxxx  06:68  xxxx 00088150
/dev/rdisk/disk7745   xx  00   00  xxxx  06:66  xxxx 00088150
/dev/rdisk/disk7747   xx  00   00  xxxx  06:64  xxxx 00088150
/dev/rdisk/disk7758   xx  00   00  xxxx  06:63  xxxx 00088150
/dev/rdisk/disk7752   xx  00   00  xxxx  03:00  xxxx 00088150
/dev/rdisk/disk7759   xx  00   00  xxxx  03:07  xxxx 00088150
/dev/rdisk/disk7751   xx  00   00  xxxx  06:67  xxxx 00088150
/dev/rdisk/disk8755   xx  00   00  xxxx  03:01  xxxx 00088150
----------------------------------------------------------------------------------------------




The output to be look like:


Code:
PKG= P8SDB     :: VGS = vgP8SOra vgP8SDB1 vgP8S001 vgP8S007 vgP8S003 vgP8S004 vgP8SOlog   :: 00060110
PKG= P9SDB     :: VGS = vgP9SOra vgP9SDB1 vgP9S001 vgP9S007 vgP9S003 vgP9S004 vgP9SOlog   :: 00088150

Thanks a lot,
Sponsored Links
    #2  
Old 01-30-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 2 September 2014, 9:22 AM EDT
Location: Galactic Empire
Posts: 3,362
Thanks: 230
Thanked 1,197 Times in 1,125 Posts

Code:
awk '/^PKG/&&!/disks/{ printf "%s", $0; f=1}f==1&&/^\//{ printf ":: %s\n",$NF; f=0;}' filename

The Following User Says Thank You to Yoda For This Useful Post:
rveri (01-30-2013)
Sponsored Links
    #3  
Old 01-30-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 2 September 2014, 8:35 AM EDT
Location: Amsterdam
Posts: 9,387
Thanks: 273
Thanked 2,349 Times in 2,108 Posts

Code:
awk '/::/{p=$0} p && /^\//{print p,"::",$NF; p=x}' file


Code:
sed -n '/::/{N;N;N;s/\n.* / :: /p;}' file


Last edited by Scrutinizer; 01-31-2013 at 06:23 PM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
rveri (01-31-2013)
    #4  
Old 01-31-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 26 August 2014, 3:05 PM EDT
Posts: 224
Thanks: 143
Thanked 13 Times in 12 Posts
bipin & Scrutinizer,

Thanks a lot,

Scrutinizer,
in the sed example ,

Code:
s/\n.*

means what, could not understand , also how you able to capture the last field i.e 00060110 or 00088150.

Thanks.
Sponsored Links
    #5  
Old 01-31-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 2 September 2014, 8:35 AM EDT
Location: Amsterdam
Posts: 9,387
Thanks: 273
Thanked 2,349 Times in 2,108 Posts
Hi, s/\n.* / :: /p means replace everything in the line buffer between the first linefeed and the last space with " :: " and print the result.

So for the first match the line buffer will contain the following after the 3 N-operations:

Code:
PKG= P8SDB     :: VGS = vgP8SOra vgP8SDB1 vgP8S001 vgP8S007 vgP8S003 vgP8S004 vgP8SOlog \nPKG= P8SDB    disks:                    \n-----------------------------------------------------------------------------------------------\n/dev/rdisk/disk7777   xx  00   00  xxxx  07:67  xxxx 00060110

so
\nPKG= P8SDB disks: \n-----------------------------------------------------------------------------------------------\n/dev/rdisk/disk7777 xx 00 00 xxxx 07:67 xxxx
will get replaced.. ( \n is representation of the linefeed in this example)

Last edited by Scrutinizer; 01-31-2013 at 06:32 PM..
Sponsored Links
    #6  
Old 02-01-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 26 August 2014, 3:05 PM EDT
Posts: 224
Thanks: 143
Thanked 13 Times in 12 Posts
Scrutinizer , much better .. Thanks again,
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk Help: Filter Multiple Entry & print in one line. rveri Shell Programming and Scripting 7 01-29-2013 11:52 PM
awk script to filter the numbers which are around the set value ks_reddy Shell Programming and Scripting 7 08-09-2012 07:34 AM
Command line / script option to filter a data set by values of one column gnat01 UNIX for Dummies Questions & Answers 1 07-04-2012 01:29 AM
AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2 right_coaster Shell Programming and Scripting 4 10-06-2011 06:07 PM
awk partial match and filter records aemunathan Shell Programming and Scripting 3 03-02-2010 10:07 PM



All times are GMT -4. The time now is 10:00 AM.