Operating System - Linux
1752793 Members
6083 Online
108789 Solutions
New Discussion юеВ

Re: getting info between 2 fields

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

getting info between 2 fields

Hello all,

I am attempting to get all information between two strings when a certain field is equal to Wed Sep 19.

here is an example of the data in the file (nb this is on AIX O/S)

LABEL: VPATH_PATH_OPEN
IDENTIFIER: F45CFAFB

Date/Time: Wed Sep 19 15:00:23 BST 2007
Sequence Number: 316081
Machine Id: 00C84A6D4C00
Node Id: pace
Class: H
Type: PERM
Resource Name: hdisk22
Resource Class: disk
Resource Type: 2107
Location: U787B.001.DNW3DCD-P1-C2-T1-W500507630518C473-L4031400800000000
VPD:
Manufacturer................IBM
Machine Type and Model......2107900
Serial Number...............75FDFW13108
EC Level.....................309
Device Specific.(Z0)........10
Device Specific.(Z1)........0303
Device Specific.(Z2)........075
Device Specific.(Z3)........30806
Device Specific.(Z4)........08
Device Specific.(Z5)........00

Description
OPEN FAILURE

Probable Causes
DISK
SCSI ADAPTER
SCSI CABLE

Failure Causes
DISK
SCSI ADAPTER
CABLE LOOSE OR DEFECTIVE

Recommended Actions
PERFORM PROBLEM DETERMINATION ON SCSI TARGET DEVICE
PERFORM PROBLEM DETERMINATION ON HOST SCSI ADAPTER
REPLACE SCSI CABLE

Detail Data
SENSE DATA
0000 0000 8000 002A 0000 0012 0000 0003 0000 0001 0000 0000 0000 0005


so when todays date is found I would like to print all information between LABEL: and SENSE DATA.

any help will be greatly appreciated.

Thanks Chris.
hello
11 REPLIES 11
lawrenzo_1
Super Advisor

Re: getting info between 2 fields

I have made a start but am struggling to get it to print between the search string:

errpt -a |awk -F":" '/Date\/Time/ {if ($2 ~ /Wed Sep 19/) {print}}'

thanks again
hello
Oviwan
Honored Contributor

Re: getting info between 2 fields

Hy

If you found the date use this.

START="LABEL"
END="SENSE DATA"
read=0

for line in $(cat yourfile) ; do

if [ "${line}" = "${START}" ]
read=1
fi

if [ ${read} -eq 1 ]
then
echo ${line}
fi

if [ "${line}" = "${END}" ]
read=0
fi

done

not tested just a brain dump

Regards
larsoncu
Advisor
Solution

Re: getting info between 2 fields

this should get you pretty close

awk '
/^LABEL/ {label= $0;}
/the date/ {printf("%s",label); print $0; date="found";}
{label =. $0; if (date ~ "found" ) print $0;}
/^SENSE/ {print $0;date = "done";}
James R. Ferguson
Acclaimed Contributor

Re: getting info between 2 fields

Hi Chris:

Try thisL:

# cat ./filter
#!/usr/bin/perl
use strict;
use warnings;

my @a;
my $toggle = 0;
my $target = shift;

while (<>) {
push @a, $_ if /^LABEL|^IDENTIFIER|^\s*$/;

if (/^Date/) {
if (/$target/) {
$toggle++;
print for @a;
@a = ();
}
else {
$toggle = 0;
@a = ();
}
}
if ($toggle) {
if (/^SENSE/) {
print;
$_ = <>;
print;
$toggle = 0;
}
else {
print;
}
}
}
1;

...Run this passing two arguments. The first is a QUOTED (single or double) string defining your target date. The second argument is the name of the file containing the data to be parsed:

# ./filter 'Sep 19' logfile

Regards!

...JRF...
larsoncu
Advisor

Re: getting info between 2 fields

this should work just as well

awk '
/^LABEL/ {label= $0;}
/the date/ {label =. $0;date = "found"}
{label =. $0;}
/^SENSE/ {label =. $0;if (date ~ "found" ) {printf("%s",label);date = "done";}
}'
Peter Nikitka
Honored Contributor

Re: getting info between 2 fields

Hi Chris,

my solution uses the strings LABEL and SENSE DATA as block markers. I store all upto the date/time line in an array, which is printed when there is a match, the rest of the block is directly printed.
The matching pattern is generated by the date command and fed into an awk variable.

awk -v m="$(date '+%b %d .* %Y')" '/^LABEL/ {what=-1;next}
what<0 {keep[lino++]=$0}
/^Date\/Time: / {if($0 ~ m) {what=1;for(i=0;ielse {what=0;lino=0} }
/^SENSE DATA/ {what=0;lino=0}
what>0' /tmp/inputdata

I would prefer to skip the line 'Detail Data' additionally - just replace
/^SENSE DATA/ {what=0;lino=0}
by
/^Detail Data {what=0;lino=0}

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
larsoncu
Advisor

Re: getting info between 2 fields

oops guess i'm mixing my perl and awk up.
anyway there is no =. in awk.

you'll need to replace the concatenations like this:
label =. $0;

should be:
label = (label $0);
larsoncu
Advisor

Re: getting info between 2 fields

now that i think about it a bit more
this should work just as well

awk '
{label = (label $0);}
/^LABEL/ {label = $0;}
/the date/ {date = "found";}
/^SENSE/ {if (date ~ "found" )
{printf("%s",label);
date = "done";}
}'
Sandman!
Honored Contributor

Re: getting info between 2 fields

Yet another way of doing the same thing.

awk '
{
if ($0~dt) flg++
if ($0~"LABEL:") start++
if ($0~"SENSE DATA")
{
if (flg) {print lst;lst="";flg=0;start=0}
else {lst="";start=0}
}
if (start) lst=(lst?lst"\n"$0:$0)
}' dt="$(date +"%a %b %d")" file