I am required to develop a script to look for specific strings in the /var/adm/messages file on our Solaris10 host. When an entry is found in the file then I need that line to be piped to a log file. I have a script with the criteria i just need help with the manipulation of the text string.
I need to replace the time stamps in /var/adm/messages and replace it with the format shown below. I need a way to replace collumn 1, 2 and 3 with the date example below.
date example:
# date '+%D %T'
01/26/11 12:40:56
I also need to make the string continue on a new line if it exceeds 100 chars, so that the log file looks pretty and not wrapped around on a standard sized monitor.
original line:
Jan 26 12:40:56 hostname usba: [ID 912658 kern.info] USB 2.0 device (usb430,a2) operating at full speed (USB 1.x) on USB 2.0 external hub: keyboard@4, hid3 at bus address 5
format I would like in my log file:
01/26/11 12:40:56 hostname usba: [ID 912658 kern.info] USB 2.0 device (usb430,a2) operating at full
01/26/11 12:40:56 speed (USB 1.x) on USB 2.0 external hub: keyboard@4, hid3 at bus address 5
Thanks, I gave it a try but I'm doing something wrong.
I am really rubbish at awk so I need some help here. Must I feed this awk command line by line of the /var/adm/messages or can I just specify the filename at the end by replacing 'inputfile'? I tried that and I got this very non-descriptive return:
awk: syntax error near line 1
awk: bailing out near line 1
cool it works, thanks
may I maybe ask you to explain the logic behind the command to me? I have run through awk docs, but this still seems a little over my head.
awk
-v yr=`date '+%Y'` # Sets the variable yr to the value current year before execution of the program begins
'BEGIN {
m["Jan"]="01"
m["Feb"]="02"
m["Mar"]="03"
m["Apr"]="04"
m["May"]="05"
m["Jun"]="06"
m["Jul"]="07"
m["Aug"]="08"
m["Sep"]="09"
m["Oct"]="10"
m["Nov"]="11"
m["Dec"]="12"
} #the BEGIN block is evaluated before awk starts processing the input file, We initialize array "m" with index as month and value as month's number
{ dt=m[$1]"/"$2"/"yr FS $3; # here $1 = Jan,$2= 26 and $3=12:40:56 as per your input file, store date as per your formate into varibale dt.
printf dt FS; # print date and FS (filed seperator)
l=18; # here l=18 , length of date and time which we alerady printed.
for(i=4;i<=NF;i++) # start for loop from field 4
{
l+=length($i)+1; # adding length of field and 1 for field seperator
if(l>100 && flg == 1) # if this condition is true means we already print 100 char.
{flg=0;l=18;printf RS dt FS $i FS} # reset the flag and length variable and print date time and field on new line
else {flg=1;printf $i FS } # condition is false then print field on same line
}
printf "\n"}' inputfile