- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- command to change date format
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 12:22 AM
тАО06-06-2006 12:22 AM
I've a log file and the each line have 5 column and want to change the date format in the 4th column.
ORIG -- A:B:C:YYYYMMDDhhmmss:E
TO -- A:B:C:YYYY-MM-DD hh:mm:ss:E
i can write a script to convert it but would like to know any simple way or command, like awk command.
THANKS!!
Warren
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 12:30 AM
тАО06-06-2006 12:30 AM
Re: command to change date format
rather messy:
http://forums1.itrc.hp.com/service/forums/bizsupport/questionanswer.do?threadId=641226
might be quicker to use cut -c
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 12:44 AM
тАО06-06-2006 12:44 AM
Re: command to change date format
... like awk solution ...
I keep lines consisting not of 5 columns and invalid date columns unchanged:
awk 'NF==5 {n=split($4,ndate,":"); if (n!=5) {print;next}
year=substr(ndate[4],1,4)
mon=substr(ndate[4],5,2)
day=substr(ndate[4],7,2)
hour=substr(ndate[4],9,2)
min=substr(ndate[4],11,2)
sec=substr(ndate[4],13)
printf("%s %s %s %s:%s:%s:%s-%s-%s %s:%s:%s:%s %s\n", $1,$2,$3,ndate[1],ndate[2],ndate[3],year,mon,day,hour,min,sec,ndate[5], $5); next}
{print}' logfile >logfile.new
mfG Peter
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 01:00 AM
тАО06-06-2006 01:00 AM
Re: command to change date format
the command does not work ... the output file is same as the input file...!?
i tried to debug but can't fix it.
Thanks,
WAR.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 01:14 AM
тАО06-06-2006 01:14 AM
Solution# perl -pe 's/(.:.:.:)(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d):(.)/$1$2-$3-$4 $5:$6:$7:$8/' file
...and if you want to replace "in-place" do:
# perl -iold -pe 's/(.:.:.:)(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d):(.)/$1$2-$3-$4 $5:$6:$7:$8/' file
...this will leave "file" updated with a backup in "file.old".
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 01:29 AM
тАО06-06-2006 01:29 AM
Re: command to change date format
how come both the solution not working on my server....!!
#perl -pe 's/(.:.:.:)(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)(\d\d):(.)/$5:$6:$7:$8/' haha.log
A:B:C:200606060606:E
....is it bcoz of 06-06-06....?? bad luck on today!?
anyway thanks a lots for the reply.
WAR.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 01:31 AM
тАО06-06-2006 01:31 AM
Re: command to change date format
Please post a snippet of your file. The solutions proposed assume the format of you posted description.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 01:43 AM
тАО06-06-2006 01:43 AM
Re: command to change date format
You posted as string like "A:B:C:200606060606:E" which looks like it does *not* include the seconds (ss) part.
For that you would need:
# perl -pe 's/(.:.:.:)(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d):(.)/$1$2-$3-$4 $5:$6:$7/' file
...for instance:
# echo " echo "A:B:C:200606060606:E"|perl -pe 's/(.:.:.:)(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d):(.)/$1$2-$3-$4 $5:$6:$7/'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 01:55 AM
тАО06-06-2006 01:55 AM
Re: command to change date format
JRF, your answner is work and date field does not have "sec".
mfG, i modified the awk and working also.
sed 's/:/ /g' FILE |
awk '{split($4,ndate);
year=substr(ndate[1],1,4)
mon=substr(ndate[1],5,2)
day=substr(ndate[1],7,2)
hour=substr(ndate[1],9,2)
min=substr(ndate[1],11,2)
printf("%s:%s:%s:%s-%s-%s %s:%s:%s\n",$1,$2,$3,year,mon,day,hour,min,$5); next}
{print}'
thx again for the reply.
WAR.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-06-2006 02:03 AM
тАО06-06-2006 02:03 AM
Re: command to change date format
A:B:C:20060606060606:E
A:B:C:20060504030201:E
A:B:C:20050403020102:E
lt09:/tmp 114 > perl -paF: -e'$F[3]=sprintf"%2d%02d-%02d-%02d %02d:%02d:%02d",unpack"(A2)*",$F[3]and$_=join":",@F' test.dta
A:B:C:2006-06-06 06:06:06:E
A:B:C:2006-05-04 03:02:01:E
A:B:C:2005-04-03 02:01:02:E
lt09:/tmp 115 >
But I think it is a stupid^wnot so wise idea, as it introduces two new ':' characters that are already used as field separators, making parsing the file harder.
Enjoy, Have FUN! H.Merijn