LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how do i convert Month to Seconds and Days are based on Month (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-convert-month-to-seconds-and-days-are-based-on-month-4175736131/)

atjurhs 04-17-2024 08:06 AM

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.

thanks for your help!

pan64 04-17-2024 08:08 AM

https://askubuntu.com/questions/1112...awk-to-seconds
https://stackoverflow.com/questions/...o-epoch-in-awk

atjurhs 04-17-2024 11:42 AM

got it, thanks

atjurhs 04-30-2024 07:35 AM

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)
}

if ($1 == "car")
{
ford=$3
}

if ($1 == "OS")
{
linux=$3
print("\n%s,%s,%s,%s,%s",puppy, kitten, time_converted_to_seconds, ford, linux)
}
}
}
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.

pan64 04-30-2024 07:48 AM

In this example better to use FS=" = " (space, equal, space)

atjurhs 04-30-2024 08:25 AM

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

michaelk 04-30-2024 09:25 AM

Something like the following without the quotes?
12:01:29.7613
Code:

split($NF,a,":")
seconds=a[1]*3600+a[2]*60+a[3]


pan64 04-30-2024 10:02 AM

Quote:

Originally Posted by atjurhs (Post 6499054)
good morning pan, the indexing works out fine when it's left as FS =" " not so good when it's FS=" = "

What does it mean? Probably you need to use $2 instead of $3. Or $NF

atjurhs 04-30-2024 10:07 AM

michaelk,

using your thought i tried
Quote:

time1 == $4
which gets me the time part 12:01:29.7613" but with the "
and then
time2 =split($NF,time1,""")
kind of like using a function, of course that didn't work

atjurhs 04-30-2024 10:16 AM

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

pan64 04-30-2024 10:21 AM

Quote:

Originally Posted by atjurhs (Post 6499073)
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.

atjurhs 04-30-2024 10:54 AM

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

Quote:

time = split($NF,$2,":")
isn't quite right

pan64 04-30-2024 11:18 AM

probably this helps: https://stackoverflow.com/questions/...n-array-in-awk


All times are GMT -5. The time now is 11:18 AM.