LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-15-2008, 12:52 AM   #1
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Rep: Reputation: 15
Convert "xxhr yymn zzs" into "xx:yy:zz"


Hey,

I ecountered another problem in my project, i have set recordings of mettings and upon command i get the length of the file int he following format:

xhr ymn zs (i.e 10hr 5mn 6s)
(which would then be converted into 10:05:06)

The main issues im having are that:

1) it is not always the format xhr ymn zs, i.e if it was only 20mins 30secs long it will only be "20mn 30s" and so the output would need to be 00:20:30

2) extracting the numbers

3) if they are single digit, add a 0 to them.

I have attempted to try and figure this out myself, in regards to issue 1, the only thing i could think of was to have 3 if statements to check if "hr" was in there first, if not then "mn" and lastly "s" - however im not sure what the equivelant operator for an "is in" type thing would be, as im only aware of == !== and the numerical operators.

In regards to the second, i think this would be an easy job with regex, however im not familiar, and my attempt only returned the first value (x), which was:

echo `expr match "16hr 5mn 3s" '\([0-9][0-9]*[0-9][0-9]*[0-9][0-9]\)'`

(note: i just hardcoded the string whilst i was experimenting)

For the third issue, i think this would be simple, as following from 2, if it were possible to set the hr min and sec ammounts in variables, i could simply do something like:

Code:
hourlength="${#hour}"
if [ "$hourlength" -lt 2 ]; then
hour=0"$hour"
fi
(and the same for mins and secs)

Im not sure if there is perhaps a better way then what i was attempting to do, if so im open to suggestions.

I am trying to learn bash scripting, so though i appreciate it alot if you could provide the code, it would help me out alot if you could just comment the script, or explain what is going on, so i can have a better understanding, though i do understand not everyone has time to explain, especially if it is a complicated concept.

Id appreciate any assistance that could be given.

Thankyou.
 
Old 11-15-2008, 10:38 AM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

here's a sed to convert the time (I inserted some comments):
Code:
jan@jack:~/tmp> echo "1hr 5mn 6s" | sed -r '
> # a time in format "Xhr Ymn Zs"
> /^[0-9]+hr/s/([0-9]+)hr ([0-9]+)mn ([0-9]+)s/\1:\2:\3/
> # a time in format "Ymn Zs"
> /^[0-9]+mn/s/([0-9]+)mn ([0-9]+)s/00:\1:\2/
> # a time in format "Zs"
> /^[0-9]+s/s/([0-9]+)s/00:00:\1/
> # replace ":X:" with ":0X:"
> s/:([0-9]:)/:0\1/g
> # replace "X:" at the beginning with "0X:"
> s/^([0-9]:)/0\1/
> # replace ":X" at the end with "0X:"
> s/:([0-9])$/:0\1/'
01:05:06
Jan
 
Old 11-15-2008, 06:17 PM   #3
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Original Poster
Rep: Reputation: 15
No Longer An Issue, Fixed.

Last edited by onesikgypo; 11-16-2008 at 04:28 AM.
 
Old 11-16-2008, 09:25 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Fine, and would you care to tell us how you fixed it? It might help other members. It is not considered good etiquette to ask for help, find out yourself and do not put in the effort to tell us and especially those people who went the way to read your post.

jlinkels
 
Old 11-16-2008, 02:43 PM   #5
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Original Poster
Rep: Reputation: 15
Sure, no probelm

the issue was that for some reason my result was returning 01:20:2 instead of 01:20:02, and i couldnt understand why because if i hardcoded the value "1hr 20min 2s" into it, it would return the correct result.

It came down to:
Code:
duration=`grep -m1 ^"Duration" "$infoonwmv"`
duration="${duration#*: *}"
though it removed all of the beginning and appeared to give the result "1hr 20mn 2s" actually returned "1hr 20mn 2s " (notice the space at the end) - as a result this was the cause of the problems with the script, so i simply scripted to remove the last character (which is a space), and so the full script is as follows:

Code:
duration=`grep -m1 ^"Duration" "$infoonwmv"`
duration="${duration#*: *}"
duration="${duration%?}"
duration=`echo "$duration" | sed -r '
/^[0-9]+hr/s/([0-9]+)hr ([0-9]+)mn ([0-9]+)s/\1:\2:\3/
/^[0-9]+mn/s/([0-9]+)mn ([0-9]+)s/00:\1:\2/
/^[0-9]+s/s/([0-9]+)s/00:00:\1/
s/:([0-9]:)/:0\1/g
s/^([0-9]:)/0\1/
s/:([0-9])$/:0\1/'`
 
Old 11-16-2008, 04:17 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Do you ever have to deal with decimal-format minutes or seconds?
ta0kira
 
Old 11-16-2008, 04:27 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Ok, thanks.

This one might be easier:

Code:
my_time="01:03:04"
date -d "$(echo "1970-01-01 $my_time" | sed s/mn/min/ | sed s/s/sec/ | sed s/hr/hour/)" +%T
Dealing with time formats is often difficult, so why not let 'date' do the work?

jlinkels
 
Old 11-16-2008, 05:06 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I was thinking that some sort of locale system should take care of problems like this, but I can't think of one that parses times.
ta0kira
 
Old 11-16-2008, 07:08 PM   #9
onesikgypo
Member
 
Registered: Jun 2008
Posts: 56

Original Poster
Rep: Reputation: 15
Im not too sure what you guys are referring to jlinkles & ta0kira - the purpose of the script was to convert xhr ymn zs into the format 00:00:00 - so that when i record a meeting, and need to show the length of the meeting for other purposes, i have it in the format thats required (which is xx:yy:zz), which jan61's script does perfectly - the grep command is what extracts this info from a bunch of other info also about the file

im not exactly sure what:
Code:
my_time="01:03:04"
date -d "$(echo "1970-01-01 $my_time" | sed s/mn/min/ | sed s/s/sec/ | sed s/hr/hour/)" +%T
does at the moment, since i am unable to test, but appears your starting with the time in xx:yy:zz format and converting it to xhour ymin zsec ?
 
Old 11-16-2008, 07:21 PM   #10
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
No, it's a stupid mistake. The script does convert Xhr Ymn Zs into HH:MM:SS, but I set the wrong value for my_time.

Code:
donald_pc:~$ my_time="2hr 3mn 4s"
donald_pc:~$ date -d "$(echo "1970-01-01 $my_time" | sed s/mn/min/ | sed s/s/sec/ | sed s/hr/hour/)" +%T
02:03:04
donald_pc:~$ my_time="3mn 4s"
donald_pc:~$ date -d "$(echo "1970-01-01 $my_time" | sed s/mn/min/ | sed s/s/sec/ | sed s/hr/hour/)" +%T
00:03:04
jlinkels
 
Old 11-16-2008, 07:23 PM   #11
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
ta0kira: 'date' does, but you have to use hour, min, sec instead of hr, mn, s. Otherwise it parses about every known format. By adding an offset to the epoch and asking just the time in the desired format I got what I needed.

jlinkels
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert "string" type to "char * " in c++ pranavojha Programming 18 06-05-2008 03:10 PM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration