- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: perl, sed, awk.. date format translation.
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
Forums
Discussions
Discussions
Discussions
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
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
10-13-2005 08:57 PM
10-13-2005 08:57 PM
This question just comes from soemthing I was working on...
I have date in a file in the following format
yyyymmddHHMM|.. other stuff here ...
I want to tranlate it into
yyyy/mm/dd|HH:MM|... other stuff here ...
I came up with a solution in perl, which after various people looked at became about 5 different (equally valid) solutions... so lets see how many we can generate.. I really want the shortest
Tim
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:13 PM
10-13-2005 09:13 PM
Re: perl, sed, awk.. date format translation.
date +'%Y/%m/%d|%H:%M|'
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:15 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:17 PM
10-13-2005 09:17 PM
Re: perl, sed, awk.. date format translation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:19 PM
10-13-2005 09:19 PM
Re: perl, sed, awk.. date format translation.
# echo "yyyymmddHHMM|.. other stuff here ..." | perl -ne 's%(....)(..)(..)(..)(.*)%$1/$2/$3|$4:$5%g;print;'
yyyy/mm/dd|HH:MM|.. other stuff here ...
To update into the same file then,
perl -pi -e 's%(....)(..)(..)(..)(.*)%$1/$2/$3|$4:$5%g;print;'
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:21 PM
10-13-2005 09:21 PM
Re: perl, sed, awk.. date format translation.
$ echo $X | awk 'function s(F,L){return substr($0,F,L)}{printf("%s/%s/%s|%s:%s|",s(1,4),s(5,2),s(7,2),s(9,2),s(11,2))}'
2005/10/14|10:22|
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:27 PM
10-13-2005 09:27 PM
Re: perl, sed, awk.. date format translation.
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:29 PM
10-13-2005 09:29 PM
Re: perl, sed, awk.. date format translation.
echo "yyyymmddHHMM|.. other stuff here ..." | awk -F"\n" '{ y=substr($1,0,4);m=substr($1,5,2);d=substr($1,7,2);h=substr($1,9,2);r=substr($1,11);print y"/"m"/"d"|"h":"r; }'
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:30 PM
10-13-2005 09:30 PM
Re: perl, sed, awk.. date format translation.
echo "$(echo ${var}|cut -c1-4)/$(echo ${var}|cut -c5-6)/$(echo ${var}|cut -c7-8)|$(echo ${var}|cut -c9-10):$(echo ${var}|cut -c11-12)"
2005/10/16|22:22
(RAC's script)
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:33 PM
10-13-2005 09:33 PM
Re: perl, sed, awk.. date format translation.
var=200510162222
echo "$(echo ${var}|cut -c1-4)/$(echo ${var}|cut -c5-6)/$(echo ${var}|cut -c7-8)|$(echo ${var}|cut -c9-10):$(echo ${var}|cut -c11-12)|$(echo ${var}|cut -c13-)"
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:35 PM
10-13-2005 09:35 PM
Re: perl, sed, awk.. date format translation.
echo "yyyymmddHHMM|.. other stuff here ..." | while read line;
do
echo "$(echo $line| cut -c1-4)/$(echo $line| cut -c5-6)/$(echo $line| cut -c7-8)|$(echo $line| cut -c9-10):$(echo $line| cut -c11-)"
done
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:35 PM
10-13-2005 09:35 PM
Re: perl, sed, awk.. date format translation.
for var in $(
echo "$(echo ${var}|cut -c1-4)/$(echo ${var}|cut -c5-6)/$(echo ${var}|cut -c7-8)|$(echo ${var}|cut -c9-10):$(echo ${var}|cut -c11-12)|$(echo ${var}|cut -c13-)"
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:40 PM
10-13-2005 09:40 PM
Re: perl, sed, awk.. date format translation.
I'm very impressed, but I am also looking dfor the shortest, neatest solution...
Keep them comming
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:43 PM
10-13-2005 09:43 PM
Re: perl, sed, awk.. date format translation.
We can not make use of for when you are having input line as
It will take one string and next like that. You must use with while loop only.
Difference:
# cat file
yyyymmddHHMM|.. other stuff here ...
# while read line;
> do
> ho $line| cut -c7-8)|$(echo $line| cut -c9-10):$(echo $line| cut -c11-)"
> done < file
yyyy/mm/dd|HH:MM|.. other stuff here ...
#
#
# for var in $(
> ho ${var}|cut -c7-8)|$(echo ${var}|cut -c9-10):$(echo ${var}|cut -c11-12)|$(echo ${var}|cut -c13-)"
> done
yyyy/mm/dd|HH:MM||..
othe/r/|:|
stuf/f/|:|
here//|:|
...//|:|
Take care. I heard that legends too make mistakes some time (Like you :))
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:56 PM
10-13-2005 09:56 PM
Re: perl, sed, awk.. date format translation.
perl -i.org -pe 's/(^[0-9][0-9][0-9][0-9])([0-9][0-9])([0-9][0-9])([0-9][0-9])([
0-9][0-9])/$1\/$2\/$3\/\|$4\:$5/'
Which eventually got refinmed to
perl -i.org -pe 's/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/$1\/$2\/$3\/\|$4\:$5/'
can anyone get shorter??
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 09:59 PM
10-13-2005 09:59 PM
Re: perl, sed, awk.. date format translation.
... I'm not too stingy on the old points... ... I'm not fussed if you made another subission...
I only want it to work, in principle (well I did fiddle with RAC's to get it to work, then everyone told me another way to refine it!!)
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 10:09 PM
10-13-2005 10:09 PM
Re: perl, sed, awk.. date format translation.
while read pattern rest;
do
echo "$(echo $pattern | perl -ne '@lt=split//;@var=(@lt[0..3],"/",@lt[4..5],"/",@lt[6..7],"|",@lt[8..9],":",@lt[10..@lt]);print @var;') $rest"
done < file
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 10:14 PM
10-13-2005 10:14 PM
Re: perl, sed, awk.. date format translation.
You are using with {pattern count} and I used directly as \(....\) like that. However, both these are memory operations required to remember on $1.. $9 variables. May take more system resource for bigger files.
RAC's solution with cut is very fast. (Hoping..).
Thread is given scripting hour to produce solutions.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 10:16 PM
10-13-2005 10:16 PM
Re: perl, sed, awk.. date format translation.
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 10:16 PM
10-13-2005 10:16 PM
Re: perl, sed, awk.. date format translation.
Which application is generating this information into a file? Is it your own application / some other thing.
If it is user manageable script then try to use date +'%Y/%m/%d|%H:%M|' instead of available one so that it will log details based on your requirement.
hth.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 10:19 PM
10-13-2005 10:19 PM
Re: perl, sed, awk.. date format translation.
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 10:21 PM
10-13-2005 10:21 PM
Re: perl, sed, awk.. date format translation.
cut is core functionality with os so that it must be faster than perl on operation surely.
I am tired...bcas of more replies to this thread itself. I am just retiring from this thread. :))
Take care.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 10:22 PM
10-13-2005 10:22 PM
Re: perl, sed, awk.. date format translation.
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 11:31 PM
10-13-2005 11:31 PM
Re: perl, sed, awk.. date format translation.
1 - raw nerver has been touched!!! I wrote the original script to put teh data into yyyy/mm/dd|HH:MM|... other stuff ... formatt. I handed over all scripts and it was decided to remove the various delimiter cahracters as it "saved space" or something equally trivial.. So now I have to post process the file back to its original format.... grrrr
2 - I'm not worried about if perl is "supposed" to be better than sed... I can test that easily on a sample file. see attached..
3 - I'll do some tests and give my results (you can too!!)
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2005 11:33 PM
10-13-2005 11:33 PM
Re: perl, sed, awk.. date format translation.
You can increase both brevity and readability by changing your forward slash delimiter to another character (e.g. "%"). This eliminates the escaping back slashes near forward slashes:
# perl -i.org -pe 's%^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})%$1/$2/$3|$4:$5%' file
I also dropped one "/" since your original substitution yielded yyyy/mm/dd/|HH:MM|...
Regards!
...JRF...