1847656 Members
4361 Online
110265 Solutions
New Discussion

Re: scripting

 
SAM_24
Frequent Advisor

scripting

Hi,
I have log file

10117 16/May/2003
10154 16/May/2003
10162 16/May/2003
10181 16/May/2003

I am trying to format the above output to change the month to number ie May to 05 and print the date in mm/dd/yy format as

10162 05/16/2003
10181 05/16/2003

Log file may contain entries for all the months.

I am not very good in scripting. I don't know how to do it.

Any help is appreciated.

Thanks.
Never quit
11 REPLIES 11
Rajeev  Shukla
Honored Contributor

Re: scripting

Hi Sam,
You can get the format you want using this command
date +'%m/%d/%Y'
Use this in your script to replace the existing with the new

A. Clay Stephenson
Acclaimed Contributor

Re: scripting

Well here's one solution built around caljd.sh.

#!/usr/bin/sh


cat - | while read NUM DATE
do
DT=$(caljd.sh -S "/" $(caljd.sh -i -S "/" -e -c ${DATE}))
echo "${NUM} ${DT}"
done


A search of the Forums should lead you to a copy of caljd.sh - make sure that you get Vrsn 2.10 - it's the latest.

Invoke as caljd.sh -u for full usage.

If it ain't broke, I can fix that.
Bill Douglass
Esteemed Contributor

Re: scripting

The attached script is a quick and dirty example using awk (actually GNU gawk, but I don't think I'm using anything that's not in awk).

Invoke as

awk -f sam.awk /path/to/logfile > /path/to/newlog



Tor-Arne Nostdal
Trusted Contributor

Re: scripting

Hi,
here's a script. It could be made more tricky but since youre not so skilled in script programming I tought this would be quite readable for you.

Save the script as f.ex. ./logfile.sh
remember to set the script to be excecutable:
chmod u+rx ./logfile.sh
And use it like this:
cat logfile | ./logfile.sh

#!/bin/sh

while read FIRSTVALUE DATEVALUE
do
DD="$(echo "$DATEVALUE"|cut -d"/" -f1)"
MM="$(echo "$DATEVALUE"|cut -d"/" -f2)"
YY="$(echo "$DATEVALUE"|cut -d"/" -f3)"
case $MM in
Jan|jan) MM=01
;;
Feb|feb) MM=02
;;
Mar|mar) MM=03
;;
Apr|apr) MM=04
;;
May|may) MM=05
;;
Jun|jun) MM=06
;;
Jul|jul) MM=07
;;
Aug|aug) MM=08
;;
Sep|sep) MM=09
;;
Oct|oct) MM=10
;;
Nov|nov) MM=11
;;
Dec|dec) MM=12
;;
*) print 'Invalid month' >&2
esac
print "$FIRSTVALUE ${MM}/${DD}/${YY}"
done
I'm trying to become President of the state I'm in...
Tor-Arne Nostdal
Trusted Contributor

Re: scripting

Here is how you can read directly from the logfile.

Add the following lines in the above script (from line 2)

#Define variables for filenames
# remember to change according to your usage.
LOGFILE="/path/to/mylogfile"
NEWLOG="--------/newlog"

# Create pipelines for input/output
exec 3<$LOGFILE
exec 1>$NEWLOG

Then change the "while read" command to be "while read -u3"
This will read from "pipe"=3 as we defined in with the exec command.

To complete you should also do a proper closing of the new file...
Add as a last line:
exec 3>-

I'm trying to become President of the state I'm in...
John Meissner
Esteemed Contributor

Re: scripting

cat file |
while read line
do
number=$(echo $line | awk '{print $1}')
day=$(echo $line | awk '{print $2}' | awk -F/ '{print $1}')
month=$(echo $line | awk '{print $2}' | awk -F/ '{print $2}')
year=$(echo $line | awk '{print $2}' | awk -F/ '{print $3}')
if [ "$month" = "Jan" ]
then
mth=01
elif [ "$month" = "Feb" ]
then
mth=02
elif [ "$month" = "Mar" ]
then
mth=03
elif [ "$month" = "Apr" ]
then
mth=04
elif [ "$month" = "May" ]
then
mth=05
elif [ "$month" = "Jun" ]
then
mth=06
elif [ "$month" = "Jul" ]
then
mth=07
elif [ "$month" = "Aug" ]
then
mth=08
elif [ "$month" = "Sep" ]
then
mth=09
elif [ "$month" = "Oct" ]
then
mth=10
elif [ "$month" = "Nov" ]
then
mth=11
elif [ "$month" = "Dec" ]
then
mth=12
fi
echo $number $mth"/"$day"/"$year >> file
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: scripting

cat file |
while read line
do
number=$(echo $line | awk '{print $1}')
day=$(echo $line | awk '{print $2}' | awk -F/ '{print $1}')
month=$(echo $line | awk '{print $2}' | awk -F/ '{print $2}')
year=$(echo $line | awk '{print $2}' | awk -F/ '{print $3}')
if [ "$month" = "Jan" ]
then
mth=01
elif [ "$month" = "Feb" ]
then
mth=02
elif [ "$month" = "Mar" ]
then
mth=03
elif [ "$month" = "Apr" ]
then
mth=04
elif [ "$month" = "May" ]
then
mth=05
elif [ "$month" = "Jun" ]
then
mth=06
elif [ "$month" = "Jul" ]
then
mth=07
elif [ "$month" = "Aug" ]
then
mth=08
elif [ "$month" = "Sep" ]
then
mth=09
elif [ "$month" = "Oct" ]
then
mth=10
elif [ "$month" = "Nov" ]
then
mth=11
elif [ "$month" = "Dec" ]
then
mth=12
fi
echo $number $mth"/"$day"/"$year >> file
done
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: scripting

sorry for the seemly double post.... i hit submit and realized that i forgot to add "done" as the last line. I tried to hit the stop button on my browser but it submitted too fast for me.
All paths lead to destiny
H.Merijn Brand (procura
Honored Contributor

Re: scripting

I refuse to make the wrong sequence, but translating month names to numbers is a breaze:

# cat blah
10117 16/May/2003
10154 16/May/2003
10162 16/May/2003
10181 16/May/2003
# perl -pe'BEGIN{@m{qw(jan feb mar apr may jun jul aug sep oct nov dec)}=("01".."12")}for$m(keys%m){s/\b$m\b/$m{lc$m}/ie}' blah
10117 16/05/2003
10154 16/05/2003
10162 16/05/2003
10181 16/05/2003
#

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
John Meissner
Esteemed Contributor

Re: scripting

procura - i especially like the use of blah in your script. very nice touch :)
All paths lead to destiny
H.Merijn Brand (procura
Honored Contributor

Re: scripting

And you probably can shorten it. I didn't go for the shortest version. This was the second try (you can see that the first was more complicated because I left a stray unneeded /e in the subst :)

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn