awk or nawk in ksh

I am trying to use either awk or nawk in ksh88 to grep the word "Reason" in multiple files and than print the lines that say "Reason" in a particular format that is different from how they would normally print. The original input is as follows:

 
export/home/gen/links/inform/10.76.113.55-AV33-13-Jun-22-10-58-56.txt:DIAG 0x92: Reason: Reboot
/export/home/gen/links/inform/10.47.142.55-AV63-13-Jun-22-11-08-34.txt:DIAG 0x92: Reason: Reboot
/export/home/gen/links/inform/10.44.612.65-AV64-13-Jun-22-16-40-43.txt:DIAG 0x92: Reason: Reboot
/export/home/gen/links/inform/10.47.512.25-AV66-13-Jun-22-17-00-44.txt:DIAG 0x92: Reason: Reboot
/export/home/gen/links/inform/10.75.26.175-AV26-19-Jun-22-19-18-29.txt:DIAG 0x92: Reason: Reset
/export/home/gen/links/inform/10.47.52.165-AV27-19-Jun-22-20-52-54.txt:DIAG 0x92: Reason: Reset
/export/home/gen/links/inform/10.73.144.153-AV72-18-Jun-22-20-53-09.txt:DIAG 0x91: Reason: Queue has 375 events. First event code 2f3790f8
/export/home/gen/links/inform/10.75.612.143-AV47-18-Jun-22-21-11-38.txt:DIAG 0x91: Reason: Queue has 375 events. First event code 2f3790f8

This is after the code is grepped for all lines containing "Reason" and then I egrep -v some things to eliminate lines that contain both "Reason" and other items. The below is the code that I have:

 
YESTER=`TZ=aaa24 date +%b"-"%d`
 
 
JET=$(find /export/home/gen/links/inform -name \*$YESTER\*)
 
grep "Reason" $JET | uniq | egrep -v "PASSTHRU|OCP|Power Button Reboot|Bootloader" | cut -d'/' -f 7 | awk -F"-" '{print $4,$5","$2","$3","$1":"$8","$1}' | awk -F":" '{print $1","$3":"$4":"$5}' | awk -F"," '{print $1","$2","$3","$4","$5","$4}' >> $OUTPUT

the problem aside from the fact that it is really long and there seems like there should be a better way to do this is that the last field gets printed on the newline.

This is the output I am getting:

 
Jun 22,AV33,13,10.76.113.55,DIAG 0x92: Reason: Reboot
,10.76.113.55
Jun 22,AV63,13,10.47.142.55,DIAG 0x92: Reason: Reboot
,10.47.142.55
Jun 22,AV64,13,10.44.612.65,DIAG 0x92: Reason: Reboot
,10.44.612.65
Jun 22,AV66,13,10.47.512.25,DIAG 0x92: Reason: Reboot
,10.47.512.25
Jun 22,AV26,19,10.75.26.175,DIAG 0x92: Reason: Reset
,10.75.26.175
Jun 22,AV27,19,10.47.52.165,DIAG 0x92: Reason: Reset
,10.47.52.165
Jun 22,AV72,18,10.73.144.153,DIAG 0x91: Reason: Queue has 375 events. First event code,10.73.144.153
Jun 22,AV47,18,10.75.612.143,DIAG 0x91: Reason: Queue has 375 events. First event code,10.75.612.143

I would like to be getting the following output:

 
Jun 22,AV33,13,10.76.113.55,DIAG 0x92: Reason: Reboot,10.76.113.55
Jun 22,AV63,13,10.47.142.55,DIAG 0x92: Reason: Reboot,10.47.142.55
Jun 22,AV64,13,10.44.612.65,DIAG 0x92: Reason: Reboot,10.44.612.65
Jun 22,AV66,13,10.47.512.25,DIAG 0x92: Reason: Reboot,10.47.512.25
Jun 22,AV26,19,10.75.26.175,DIAG 0x92: Reason: Reset,10.75.26.175
Jun 22,AV27,19,10.47.52.165,DIAG 0x92: Reason: Reset,10.47.52.165
Jun 22,AV72,18,10.73.144.153,DIAG 0x91: Reason: Queue has 375 events. First event code 2f3790f8,10.73.144.153
Jun 22,AV47,18,10.75.612.143,DIAG 0x91: Reason: Queue has 375 events. First event code 2f3790f8,10.75.612.143

Those last two lines should all be on one line...they were just too long to fit here.
Any help is greatly appreciated!!

---------- Post updated at 05:30 PM ---------- Previous update was at 05:20 PM ----------

Please ignore my last comment about the last two lines being on one line, the output as shown above is exactly how I want it to be

i tested with linux/bash. sorry don't have access to Solaris machine from home..

#!/bin/sh
filelist=$(find ./inform -name *.txt -print | tr '\n' ' ')
echo "Files $filelist"

nawk -F':' '
$2 ~ /Reason/ && $3 !~ /(PASSTHRU|OCP|Power Button Reset|Bootloader)/ {
        split(FILENAME, a, "-")
        f = a[1]
        while (i = index(f, "/")) f = substr(f, i+1)
        printf("%s %s,%s,%s,%s,%s,%s\n", a[4], a[5], a[2], a[3], f, $0, f)
}' $filelist

Thanks for your response. Its giving me the following error:

 
TrainingScript.sh: syntax error at line 62: `filelist=$' unexpected

neutronscott used "#! /bin/sh". On Solaris that is the old Bourne shell. Switch it to ksh.

I tried that but that gives me this error:

 
 
find: bad option Scriptingnotes.txt
find: [-H | -L] path-list predicate-list
Files

Try changing that line:

filelist=$(find ./inform -name \*.txt -print | tr '\n' ' ')
1 Like

Okay so that works for pulling all of the files that end in .txt. However, I have created a variable for yesterday and I am trying to only find the files that contain yesterday in the filename. So when I try this code, it doesn't seem to work.

 
YESTER=`TZ=aaa24 date +%b"-"%d`
filelist=$(find /export/home/gen/links/inform -name \*$YESTER -print | tr '\n' ' ')
echo "Files $filelist"
nawk -F':' '
$2 ~ /Reason/ && $3 !~ /(PASSTHRU|OCP|Power Button Reset|Bootloader)/ {
split(FILENAME, a, "-")
f = a[1]
while (i = index(f, "/")) f = substr(f, i+1)
printf("%s %s,%s,%s,%s,%s,%s\n", a[4], a[5], a[2], a[3], f, $0, f)
}' $filelist

---------- Post updated at 10:27 AM ---------- Previous update was at 10:24 AM ----------

Nevermind, I just got that to work by doing this:

 
 
filelist=$(find /export/home/gen/links/inform -name \*$YESTER\* -print | tr '\n' ' ')

However I am having some trouble directing this to an $OUTPUT file variable....any idea as to where I should put the >>?

Should do it

}' $filelist >> $OUTPUT

that works for sending it to output, but its still putting those IP address on the next line below like so:

 
Files /export/home/gen/links/inform/10.7.533.44-NDS-AV34-1-Jun-23-1 Jun,NDS,AV26,10.7.533.44,DIAG 0x92: Reason: OCAP initiated reset...
,10.7.533.44
1 Jun,AV23,10.7.142.54,DIAG 0x92: Reason: OCAP initiated reset..
,10.7.142.54

In addition, its printing a long line of all the filenames at the top which I would like to remove.

Must be DOS formatted. I had a line in there for testing, you can remove the "echo $filelist"

YESTER=`TZ=aaa24 date +%b"-"%d`
filelist=$(find /export/home/gen/links/inform -name \*$YESTER -print | tr '\n' ' ')
nawk -F':' '
$2 ~ /Reason/ && $3 !~ /(PASSTHRU|OCP|Power Button Reset|Bootloader)/ {
split(FILENAME, a, "-")
f = a[1]
while (i = index(f, "/")) f = substr(f, i+1)
sub("\r$", "");
printf("%s %s,%s,%s,%s,%s,%s\n", a[4], a[5], a[2], a[3], f, $0, f)
}' $filelist >> $OUTPUT
1 Like

Thanks!! Works wonderfully!