Operating System - HP-UX
1847253 Members
4019 Online
110263 Solutions
New Discussion

Re: Urgent help in scripting

 
SOLVED
Go to solution
SAM_24
Frequent Advisor

Urgent help in scripting

Hi,

I need to replace the date format in the access log file from

word dd/mm/yy to
word mm/dd/yy

How to accomplish using any one of these tools sed or awk or perl?

Any help is appreciated.

Thanks.
Never quit
11 REPLIES 11
Michael Tully
Honored Contributor

Re: Urgent help in scripting

Try this:

$ sed 's/mm\dd/dd\/mm/' outputfile

Regards
Michael
"When I have trouble spelling, it's called fat finger syndrome"
Anyone for a Mutiny ?
SAM_24
Frequent Advisor

Re: Urgent help in scripting

Tully,

I think I didn't explain well.

My log file contains data :

user1 15/05/2003
user3 20/04/2003
user1 23/03/2003

Your suggestion just changes the letters mm/dd.

Thanks.
Never quit
Jordan Bean
Honored Contributor

Re: Urgent help in scripting

perl -pi.bak -e 's/(\s)(\d\d)\/(\d\d)\//\1\3\/\2\//;' logfile
Jordan Bean
Honored Contributor

Re: Urgent help in scripting

perl -pi.bak -e 's[(\s)(\d\d)/(\d\d)/(\d\d)][\1\3/\2/\4];' logfile
malay boy
Trusted Contributor

Re: Urgent help in scripting

Hi,
Try this :
cat test|while read u1 u2
do
date1=`echo $u2|cut -f1 -d/ `
date2=`echo $u2|cut -f2 -d/ `
date3=`echo $u2|cut -f3 -d/ `
echo $u1 $date2"/"$date1"/"$date3
done

test is your filename.

regards
mB
There are three person in my team-Me ,myself and I.
Balaji N
Honored Contributor

Re: Urgent help in scripting

[balajin@redpenguin tmp]$ cat a.txt
abcd 10/11/03
abcd 10/12/03
abcd 10/13/03
abcd 10/14/03
abcd 10/15/03
abcd 10/16/03
[balajin@redpenguin tmp]$ awk '{a=$1; b=substr($2,0,2);c=substr($2,4,2); d=substr($2,6);print a " " c "/" b d}' a.txt
abcd 11/10/03
abcd 12/10/03
abcd 13/10/03
abcd 14/10/03
abcd 15/10/03
abcd 16/10/03


-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Jean-Louis Phelix
Honored Contributor
Solution

Re: Urgent help in scripting

Hi,

This sed will do it :

input :

user1 15/05/2003
user3 20/04/2003
user1 23/03/2003

sed 's;\([0-9][0-9]\)/\([0-9][0-9]\)\(/[0-9][0-9][0-9][0-9]\);\2/\1\3;'

output :

user1 05/15/2003
user3 04/20/2003
user1 03/23/2003

Regards.
It works for me (© Bill McNAMARA ...)
Leif Halvarsson_2
Honored Contributor

Re: Urgent help in scripting

Hi,
Another awk example:

while read a b
do
echo $a `echo $b |awk 'BEGIN { FS = "/"; OFS = "/" }
{ print $2,$1,$3 }'`
done
John Meissner
Esteemed Contributor

Re: Urgent help in scripting

cat file |
while read line
do
var1=$(echo $line | awk '{print $1}'
var2=$(echo $line | awk '{print $2}' | awk -F/ '{print $1}'
var3=$(echo $line | awk '{print $2}' | awk -F/ '{print $2}'
var4=$(echo $line | awk '{print $2}' | awk -F/ '{print $3}'

echo $var1 $var3"/"$var2"/"$var4 >> newfile
done
All paths lead to destiny
SAM_24
Frequent Advisor

Re: Urgent help in scripting

Thank you all.

You are all masters!!!
Never quit
Francisco J. Soler
Honored Contributor

Re: Urgent help in scripting

Hi,
Yes, i know it, it is too late, but i was traveling the last days. Anywere, this is my answer:

awk -F"[ \t/]" '{printf("%s %s/%s/%s\n",$1,$3,$2,$4)}' logfile

Cheers.

Frank.
Linux?. Yes, of course.