LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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: 312

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,869

Rep: Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314
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: 312

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

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

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.
 
Old Today, 07:48 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,869

Rep: Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314Reputation: 7314
In this example better to use FS=" = " (space, equal, space)
 
  


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 08:07 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