cesarNZ
September 3, 2007, 11:19pm
1
hi all,
am trying to 'grep' some text from a log file and use the 'cut' command to read from that line i just grep'ed to extract date/time and response times.
code sniplet i am using is :
grep -i 'text to grep' Out.log |
while read LINE;
do
mydate=$LINE | cut -f1 -d] | cut -f2 -d[ | cut -f1 -dN;
res=$LINE | cut -f2 -d] | cut -f2 -d[;
printf "%-16s %s\\n" $mydate $res
done;
the output i want printed to the terminal in the following columns :
date time response
unfortunately this is printing empty values to the terminal.
the cut command i am using works since i echo'ed the output to the terminal. i think the problem is using printf to print since i am new to printf.
any suggestions on what could be wrong ??
thanks in advance,
Cesar.
its easier if you show how your input file looks like, and also what you expect to see
cesarNZ
September 4, 2007, 12:28am
3
hi,
my input file looks something like this:
[9/4/07 16:03:46:222 NZST] 962cea SystemOut O RESPONSE [224 millis]
[9/4/07 16:04:28:820 NZST] 962cea SystemOut O RESPONSE [127 millis]
[9/4/07 16:05:05:190 NZST] 16e39ea SystemOut O RESPONSE [424 millis]
[9/4/07 16:05:05:466 NZST] 16e39ea SystemOut O RESPONSE [254 millis]
[9/4/07 16:05:42:547 NZST] 660d07 SystemOut O RESPONSE [199 millis]
i wanted to print out the variables under the following columns :
date/time | response
9/4/07 16:05:42:547 | 199
with GNUawk
awk '{ b=gensub(/\[(.+)\].*\[(.+)\]/,"\\1 \\2","g",$0) ;print b }' "file"
output:
# ./test.sh
9/4/07 16:03:46:222 NZST 224 millis
9/4/07 16:04:28:820 NZST 127 millis
9/4/07 16:05:05:190 NZST 424 millis
9/4/07 16:05:05:466 NZST 254 millis
9/4/07 16:05:42:547 NZST 199 millis
cesarNZ
September 4, 2007, 5:32pm
5
thanks for the reply, got one more problem. I don't have any write access to this box where i will be running this script so i can't take the grep command i am using and write it out to a file. would something like this work :
awk '{ b=gensub(/\[(.+)\].*\[(.+)\]/,"\\1 \\2","g",$0) ;print b }' "grep -i \'text to grep\' Out.log "
cesarnz:
thanks for the reply, got one more problem. I don't have any write access to this box where i will be running this script so i can't take the grep command i am using and write it out to a file. would something like this work :
awk '{ b=gensub(/\[(.+)\].*\[(.+)\]/,"\\1 \\2","g",$0) ;print b }' "grep -i \'text to grep\' Out.log "
awk '/text to grep/{ b=gensub(/\[(.+)\].*\[(.+)\]/,"\\1 \\2","g",$0) ;print b }' file
cesarNZ
September 4, 2007, 11:13pm
7
hummm didnt quite work for me, think my version of awk may be the problem but i dont understand awk well enough to say that. Do you know of any good tutorials for awk ?? i tried what you suggested and it gives me an error which looks like :
awk: syntax error near line 1
awk: illegal statement near line 1
possibly the gensub method is causing this problem??
awk '{ gsub(/\[|\]/,"");print $1,$2,$(NF-1),$NF }' "file"
cesarNZ
September 5, 2007, 12:21am
9
sorry dude this just isnt working for me, heres my script :
#!/bin/ksh
set -A CLUSTER 1 2 3
i=0;
while [ i -lt 3 ];
do
cd /log/logdir${CLUSTER[$i]}
grep -i 'RESPONSE' Out.log > /tmp/tmpfile.log
cd /tmp/
/usr/bin/awk '{ gsub(/\[|\]/,"");print $1,$2,$(NF-1),$NF }'"tmpfile.log"
(( i=i+1 )) ;
done;
output i get is :
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
cesarnz:
/usr/bin/awk '{ gsub(/\[|\]/,"");print $1,$2,$(NF-1),$NF }'"tmpfile.log"
dude, leave a space between the awk parameters and the file name.
cesarNZ
September 5, 2007, 5:11pm
11
sorry that was a typo was trying to rush it, the line is actually as follows :
/usr/bin/awk '{ gsub(/\[|\]/,"");print $1,$2,$(NF-1),$NF }' "tmpfile.log"
and i still get the same error, any more suggestions ?
cesarNZ
September 5, 2007, 7:19pm
12
hi,
actually i got around the problem by using a while read LINE; loop. thanks for all your help.