Question

I am using

awk '/^E/ {a=substr($0,1,26);n=NR} NR==n+2 && n {print n,a,$0}' logfile

but i want to pass the either date or timestamp as parameter to the shell script coming after E(want to fetch line which always starts with E). suppose if i give input as 2014-04-03 to the below file(2 lines as per below input file), it should fetch line starting with E and match the date and display the output for all the entries of that date

Expected output

line number | E 2014-04-03 12:11:41.853 |location=PIN_ERR_FM:5 class=PIN_ERR_APPLICN:4 errno=PIN_ERR_VALUE:46

inputfile is like below

E 2014-04-03 12:11:41.853  abm310  cm:21.-142726944  functionalpurchase.c:1821 1:Isa-P
        funcitional_aply_elmt eror
        <location=PIN_ERR_FM:5 class=PIN_ERR_APPLICN:4 errno=PIN_ERR_VALUE:46
        <field num=PIN__T:8,62 recid=0 reserved=0 reserved2=0 time(sec:usec)=1396548701:>

D 2014-04-03 12:11:41.853  abm310  cmf:2.-142726944  funtionalpurchase.c:1767 1:Isa-PC:
        Now apply the good fee
        <location=debugstate.c;class not found;errno=not found:01

E 2014-04-03 12:11:41.853  abm310  cm:21.-142726944  functionalpurchase.c:1821 1:Isa-P
        <location=PIN_ERR_FM:5 class=PIN_ERR_APPLICN:4 errno=PIN_ERR_VALUE:47>
        <field num=PIN__T:8,62 recid=0 reserved=0 reserved2=0 time(sec:usec)=1396548701:>
        <facility=0 msg_id=85 version=1>

Note: the line starting as <location... is also to be fetched for that sometime going to 2nd line or 3rd line or to 4th line after pattern match.

Was it helpful?

Solution 2

Is this some you like?

awk '/^E/ {a=$0;n=NR} NR==n+2 && n {print n,a,$0}' file
1 E 45907:000:39   Error type, meant to find solution

This prints the line number for the line starting with E, then the line, then second line after that.

Updated to get only 7 characters from trigger line.

awk '/^E 20[0-9][0-9]/ {d=$1 FS $2 FS $3;f=1;n=NR} f && /location/ {$1=$1;gsub(/[<>]/,"");print n,d,$0;f=0}' OFS=\| 1|E 2014-04-03 12:11:41.853|location=PIN_ERR_FM:5|class=PIN_ERR_APPLICN:4|errno=PIN_ERR_VALUE:46 10|E 2014-04-03 12:11:41.853|location=PIN_ERR_FM:5|class=PIN_ERR_APPLICN:4|errno=PIN_ERR_VALUE:47


From your new data I only print data if line starts with E (could be D if you like or any):

awk '/^E 20[0-9][0-9]/ {d=$1 FS $2 FS $3;f=1;n=NR} f && /location/ {$1=$1;gsub(/[<>]/,"");print n,d,$0;f=0}' OFS=\| file
1|E 2014-04-03 12:11:41.853|location=PIN_ERR_FM:5|class=PIN_ERR_APPLICN:4|errno=PIN_ERR_VALUE:46
10|E 2014-04-03 12:11:41.853|location=PIN_ERR_FM:5|class=PIN_ERR_APPLICN:4|errno=PIN_ERR_VALUE:47

OTHER TIPS

This might work for you (GNU sed):

sed -rn '/^E/{=;N;N;s/^(.{15}).*\n.*\n\s*/\1/p}' file | sed 'N;s/\n/ /'

This prints the line number of the required line and the then reads and appends the next two lines and returns the extracted strings. The output is piped to another sed which reduces every two lines to a single line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top