ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I have a file with approximately 16 million rows and need to insert date/time stamps for each row in 1 hour increments for an entire year and then starting over when a new value is in the first column.
I thought about converting it to epoch and then back but had difficulty with that. But I noticed perl had some date/time functions that might work well. Any thoughts on how this might be accomplished?
First of all, I'd stuff that data into a SQLite database file and do further manipulations with it that way.
Then, treating this file as the "database" that it in fact is, you'll need to write a short computer-program using some language other than "Bash scripting."
This works but continues to the next years until the end of the file. I would like it to restart the date/time when a new value is in the first column. The first column is already sorted so I'm thinking uniq could be used for this. Any thoughts on doing that?
That's good info Daniel and I'm going to take a look at that to see if I can figure out how to adjust. However my desired outcome is the following in your example:
Code:
1 2011 apple
2 2011 pear
3 2011 peach
1 2012 blue
2 2012 red
3 2012 yellow
1 2013 salty
2 2013 sweet
3 2013 bitter
4 2013 sour
1 2014 honda
2 2014 toyota
3 2014 nissan
After playing around a bit, I was able to use the following:
This works but continues to the next years until the end of the file. I would like it to restart the date/time when a new value is in the first column. The first column is already sorted so I'm thinking uniq could be used for this. Any thoughts on doing that?
Yes, test if $1 has changed, for this you have to compare $1 with a value, as this value will change you need a variable to store this value.
So you have to test $1 each time with a variable value, if changed reset hour to 1
Yes, test if $1 has changed, for this you have to compare $1 with a value, as this value will change you need a variable to store this value.
So you have to test $1 each time with a variable value, if changed reset hour to 1
Ok, it seems you may be a few steps ahead of me (thanks for your patience). If i'm understanding you correctly, I need to store the first $1 as a user defined variable and add the command to check $1 against the stored variable. If it matches, continue but if it doesn't, then reset the counter, overwrite the variable, and continue. Is that what you're saying?
I appreciate you forcing me to understand it and figure it out Daniel...though frustrating, i'm sure i'll be better off for it. Anyway I think i understand what your code is doing by checking if $1 does not equal save as well as saving the $1 value and finally incrementing the value for j which is initially set at 0. It seems that the mktime and strftime are throwing me off now (perhaps due to lack of understanding). I mangled together the following code with the included errors:
Code:
awk 'BEGIN {FS=OFS=",";hour=1;if ($1!=u) timestamp=mktime("2017 01 01 00 00 00");u=$1;(strftime("%D %R", timestamp + hour++ * 60 * 60))}{print $1,(strftime("%D %R", timestamp)),$2;}' input.csv > output.csv
awk: line 2: function strftime never defined
awk: line 2: function strftime never defined
awk: line 2: function mktime never defined
I seem to recall getting the error about defining one or both of those before but it seems they're defined here. what am i missing?
I appreciate you forcing me to understand it and figure it out Daniel...though frustrating, i'm sure i'll be better off for it. Anyway I think i understand what your code is doing by checking if $1 does not equal save as well as saving the $1 value and finally incrementing the value for j which is initially set at 0. It seems that the mktime and strftime are throwing me off now (perhaps due to lack of understanding). I mangled together the following code with the included errors:
Code:
awk 'BEGIN {FS=OFS=",";hour=1;if ($1!=u) timestamp=mktime("2017 01 01 00 00 00");u=$1;(strftime("%D %R", timestamp + hour++ * 60 * 60))}{print $1,(strftime("%D %R", timestamp)),$2;}' input.csv > output.csv
awk: line 2: function strftime never defined
awk: line 2: function strftime never defined
awk: line 2: function mktime never defined
I seem to recall getting the error about defining one or both of those before but it seems they're defined here. what am i missing?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.