Adding the date to the output file name of an awk script
by sean mckinney from LinuxQuestions.org on (#5RNZ2)
Part 1)
I have a script the scans numerous csv's in a directory. The script selects various entries from those csv's and outputs the results to another csv in a sub directory.
I want to, via the script, include the date in the name of the output file.
Currently I have the date typed into the script, in the file name. Obviously I have to change that date every differing date that I run the script.
The current script looks like
gawk -F, '{ do various things } ' *.csv > output/filename_01-11-2021.csv
So, if I ran the script today I would have to manually change the "01-11-2021" to "08-11-2021" etc.
From what I have found so far it seems the automation would be via the use of the system-variable?? "date" but I can not figure out how to use it.
Question, if "date" is what I need to use, is the date recorded/set when the script is started or when it finishes or what? The script takes a while to run and has in the past straddled midnight.
Part 2)
In addition. I want to then process the above output file and out put the results to a different csv. Again I have the script to do this but the dates have to be changed every day that I run the script.
Using the above output file as the input for the second script the second script currently looks like
cd /output
gawk '{ do other various things } ' filename_01-11-2021.csv > filename2_01-11-2021.csv
As it tends to be late when I run the first script, the date may have changed whilst the first script was running. It might be better if the second script took the latest csv in the directory "output" as the input rather than looking for a name containing the value of "date".
I am on a windows computer at the moment but am wondering if something like the following would work
cd /output
ls -tr | tail -n1 |gawk '{ do other various things } ' > filename2_????.csv
where ???? represents the date as obtained in Part 1).
I would be grateful if someone could tell me how to bring about part 1), using, if feasible, the given script as the staring point.
Thank you.
Thanks
I have a script the scans numerous csv's in a directory. The script selects various entries from those csv's and outputs the results to another csv in a sub directory.
I want to, via the script, include the date in the name of the output file.
Currently I have the date typed into the script, in the file name. Obviously I have to change that date every differing date that I run the script.
The current script looks like
gawk -F, '{ do various things } ' *.csv > output/filename_01-11-2021.csv
So, if I ran the script today I would have to manually change the "01-11-2021" to "08-11-2021" etc.
From what I have found so far it seems the automation would be via the use of the system-variable?? "date" but I can not figure out how to use it.
Question, if "date" is what I need to use, is the date recorded/set when the script is started or when it finishes or what? The script takes a while to run and has in the past straddled midnight.
Part 2)
In addition. I want to then process the above output file and out put the results to a different csv. Again I have the script to do this but the dates have to be changed every day that I run the script.
Using the above output file as the input for the second script the second script currently looks like
cd /output
gawk '{ do other various things } ' filename_01-11-2021.csv > filename2_01-11-2021.csv
As it tends to be late when I run the first script, the date may have changed whilst the first script was running. It might be better if the second script took the latest csv in the directory "output" as the input rather than looking for a name containing the value of "date".
I am on a windows computer at the moment but am wondering if something like the following would work
cd /output
ls -tr | tail -n1 |gawk '{ do other various things } ' > filename2_????.csv
where ???? represents the date as obtained in Part 1).
I would be grateful if someone could tell me how to bring about part 1), using, if feasible, the given script as the staring point.
Thank you.
Thanks