|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
Code:
awk '/::/{p=$0} p && /^\//{print p,"::",$NF; p=x}' fileCode:
sed -n '/::/{N;N;N;s/\n.* / :: /p;}' fileLast 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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
Scrutinizer , much better .. Thanks again,
|
| Sponsored Links | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|