Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 01-30-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 4 December 2016, 7:33 PM EST
Posts: 226
Thanks: 143
Thanked 15 Times in 14 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 Unix and Linux 01-30-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 5 December 2016, 4:51 PM EST
Location: Galactic Empire
Posts: 3,529
Thanks: 248
Thanked 1,281 Times in 1,203 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 Unix and Linux 01-30-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 11 December 2016, 7:49 AM EST
Location: Amsterdam
Posts: 11,109
Thanks: 440
Thanked 3,111 Times in 2,751 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 07:23 PM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
rveri (01-31-2013)
    #4  
Old Unix and Linux 01-31-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 4 December 2016, 7:33 PM EST
Posts: 226
Thanks: 143
Thanked 15 Times in 14 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 Unix and Linux 01-31-2013
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 11 December 2016, 7:49 AM EST
Location: Amsterdam
Posts: 11,109
Thanks: 440
Thanked 3,111 Times in 2,751 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 07:32 PM..
Sponsored Links
    #6  
Old Unix and Linux 02-01-2013
rveri rveri is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 4 December 2016, 7:33 PM EST
Posts: 226
Thanks: 143
Thanked 15 Times in 14 Posts
Scrutinizer , much better .. Thanks again,
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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-30-2013 12:52 AM
awk script to filter the numbers which are around the set value ks_reddy Shell Programming and Scripting 7 08-09-2012 08: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 02: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 07:07 PM
awk partial match and filter records aemunathan Shell Programming and Scripting 3 03-02-2010 11:07 PM



All times are GMT -4. The time now is 09:45 AM.