how do i convert Month to Seconds and Days are based on Month
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
how do i convert Month to Seconds and Days are based on Month
hi,
i'm writing an AWK script in which i need to convert one of my data field to total seconds. the data field is written in the file as
Code:
"day-month-2024 hour:minutes:seconds"
for example "17-apr-2024 08:58:45.6789"
i know how to convert a single day, the year, the hour, and the minutes. what i don't know how to convert to seconds is the month because that changes the number of Days.
so i'm back to this problem again. i didn't have it solved like i thought i did. it is tougher than i thought. the time string gets read when running a bigger piece of the code. here's my example code, and the time string that i need to convert is "30-April-2024 12:01:29.7613" really i only need to convert the 12:01:29.7613 to seconds
Quote:
here's an example input file
dog = brown
cat = black
clock = "30-April-2024 12:01:29.7613"
car = ford
OS = linux
Quote:
BEGIN{
FS =" "
}
{
while (getline==1)
{
if ($1 == "dog")
{
puppy=$3
}
if ($1 == "cat")
{
kitten=$3
}
if ($1 == "clock")
{
time = "30-April-2024 12:01:29.7613"
time_converted_to_seconds = some_function(time)
}
so for the time field if i just say time == $3 i get "30-April-2024
and if i say time == $4 i get 12:01:29.7613" which has both the : and a " as part of the string.
i also tried mktime but that just gets me -1
once 12:01:29.7613" is broken down into it's hours, minutes, seconds pieces i can do the math to get total number of seconds, i just don't know how to break it down.
good morning pan, the indexing works out fine when it's left as FS =" " not so good when it's FS=" = "
and that doesn't help me parse out the "30-April-2024 12:01:29.7613" time
but thanks for your idea
pan, the script works fine (parsing out lots of fields) except for parsing out the time field, which has the : and the " There is no need to change the Field Separator to something other than what it is, that would mess-up the rest of the script
pan, the script works fine except (parsing out lots of fields) except for parsing out the time field, which has the : and the " There is no need to change the Field Separator to something other than what it is, that would mess-up the rest of the script
That is wrong. Your problem is that the field separator splits that time value into different parts. If you want to solve it you need to change the field separator. Or make an ugly workaround, like time = $4 + $5 or something similar. But anyway, you need to decide if you want to split it or not, you cannot do both in the same time.
You can also re-evaluate/re-split $0 if $1 = time.
ok pan i see your reasoning, thank you! so i used your FS=" = " field separator and renumbered all the $3 to $2 and i can get the same output for all the non-time fields that i got before AND now for the time field which is $2 i get no quotes
Quote:
30-April-2024 12:01:29.7613
this is one step closer
so now i just need to throw away the DD-MM-YYYY which somehow i should be able to key off of the space and then break the time array into seconds. i would think something like what michaelk was suggesting would work, but i don't know the right syntax
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.