LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-17-2024, 08:06 AM   #1
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Rep: Reputation: Disabled
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!

Last edited by atjurhs; Yesterday at 01:46 PM.
 
Old 04-17-2024, 08:08 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

Rep: Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315
https://askubuntu.com/questions/1112...awk-to-seconds
https://stackoverflow.com/questions/...o-epoch-in-awk
 
Old 04-17-2024, 11:42 AM   #3
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
got it, thanks
 
Old Today, 07:35 AM   #4
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
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.

Last edited by atjurhs; Today at 08:21 AM.
 
Old Today, 07:48 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

Rep: Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315
In this example better to use FS=" = " (space, equal, space)
 
Old Today, 08:25 AM   #6
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
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
 
Old Today, 09:25 AM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,717

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Something like the following without the quotes?
12:01:29.7613
Code:
split($NF,a,":")
seconds=a[1]*3600+a[2]*60+a[3]

Last edited by michaelk; Today at 09:26 AM.
 
Old Today, 10:02 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

Rep: Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315
Quote:
Originally Posted by atjurhs View Post
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
 
Old Today, 10:07 AM   #9
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
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
 
Old Today, 10:16 AM   #10
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
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

Last edited by atjurhs; Today at 10:18 AM.
 
Old Today, 10:21 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

Rep: Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315Reputation: 7315
Quote:
Originally Posted by atjurhs View Post
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.
 
Old Today, 10:54 AM   #12
atjurhs
Member
 
Registered: Aug 2012
Posts: 316

Original Poster
Rep: Reputation: Disabled
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

Last edited by atjurhs; Today at 11:11 AM.
 
Old Today, 11:18 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,876

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


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
LXer: GNU/Linux Saw Its Biggest Global Boost This Month, Rising 0.6% in a Month Based on statCounter LXer Syndicated Linux News 0 12-27-2023 05:03 PM
convert total no of seconds in the format hour minutes and seconds suchi_s Programming 15 03-15-2011 11:34 AM
[SOLVED] Experiencing freezing up to 5 seconds every 10 seconds, could these be the problem? Switch7 Slackware 10 11-16-2009 04:36 PM
hdparm power save, 15 seconds of spin, 5 seconds of rest... Romanus81 Linux - Laptop and Netbook 1 01-01-2009 05:24 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:17 PM.

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