LinuxQuestions.org
Help answer threads with 0 replies.
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 01-23-2018, 07:19 PM   #16
nooobeee
Member
 
Registered: Oct 2004
Distribution: Debian
Posts: 72

Original Poster
Rep: Reputation: 1

Yep, that did it, but now the date's messed up. Oh well, making progress. I'll take another stab at 'er in the morning.

FYI, if anyone else finds this post, resolution to "function strftime never defined" and "function mktime never defined" on stock ubuntu is the following:

Code:
sudo apt-get install gawk
 
Old 01-23-2018, 07:19 PM   #17
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Quote:
Originally Posted by nooobeee View Post
oh wait, i think i need to install gawk or something like that...i'm pretty sure i had this on my system at work and that's how i addressed it
Yes strftime() and mktime() are in gawk version
 
1 members found this post helpful.
Old 01-23-2018, 07:27 PM   #18
nooobeee
Member
 
Registered: Oct 2004
Distribution: Debian
Posts: 72

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by keefaz View Post
Close, but you moved things in wrong places

awk syntax is like: condition {action}
so:
Code:
awk '
BEGIN {FS=OFS=",";timestamp=mktime("2017 01 01 00 00 00")}
$1!=u {hour=1;u=$1}
{print $1, strftime("%D %R", timestamp + hour++ * 60 * 60),$2, $3}
' input.csv
BEGIN{}: initialize block
$1 != u: condition
if condtion is true then next block: {hour=1;u=$1}
and following block is executed without condition
I'm not sure i understand the hour=1 part. Is that the part that resets the timestamp? If so, i don't see how.
 
Old 01-23-2018, 07:29 PM   #19
nooobeee
Member
 
Registered: Oct 2004
Distribution: Debian
Posts: 72

Original Poster
Rep: Reputation: 1
BTW, that code did work, but I'd like to understand that part about hour=1. Is that a function of mktime and strftime?
 
Old 01-23-2018, 08:09 PM   #20
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
No that resets the hour counter, hour is just a variable (any name will do)

see gawk time function
https://www.gnu.org/software/gawk/ma...Functions.html

mktime creates a timestamp, eg a number in seconds from Unix epoch (1970 00:00:00 GMT)
1 hour is 60 * 60 seconds, so with hour = 1:
timestamp + hour++ * 60 * 60 => added 1 hour to timestamp value and hour is incremented by 1 after the operation (hour++, hour = 2)

Edit: timestamp is never reseted, it has the same value during all the program execution

Last edited by keefaz; 01-23-2018 at 08:16 PM.
 
1 members found this post helpful.
Old 01-23-2018, 08:17 PM   #21
nooobeee
Member
 
Registered: Oct 2004
Distribution: Debian
Posts: 72

Original Poster
Rep: Reputation: 1
Ahhh, ok, that makes sense of the *60*60. Thanks for the link and explanation...i'll do some more reading and test this out in my sandbox tomorrow and report back. Thanks for all your help.
 
  


Reply

Tags
awk, csv, gawk



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
increment a value of string each time hruday Linux - Newbie 1 11-23-2017 12:22 PM
bash: time difference and calculate the mid day of the day? frenchn00b Programming 3 05-03-2009 02:03 PM
Easy string/text manipulation/indentation for restructured text brianmcgee Linux - Software 1 04-22-2008 08:27 PM
need help with text manipulation pcorajr Programming 12 12-15-2006 07:33 AM
More text manipulation ice_hockey Linux - General 2 05-28-2005 01:43 AM

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

All times are GMT -5. The time now is 09:39 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