CAT multiple files with filename pattern
Hello,
I have the following scenario: an application is creating each day a file that is having some records in plain text. The files are being saved with the following name: Code:
auto_test_01-Jan-2019.txt Can someone help me build up this bash in order for the requirement to be achieved? I have tried some ideas but I could not get it to work as expected (either it shows the output for the first file, either is showing the output for the last file multiple time). My knowledge in bash scripting is at the beginner level, so please bare with me. :) Thanks! |
Quote:
Code:
read -p "Enter starting date" s_date Quote:
Code:
a=2 Quote:
Code:
list=(auto_test_01-Jan-2019.txt |
my draft
Code:
#!/bin/bash |
Unsure if my initial post was too clear:
The files that are in dir1 are: Code:
auto_test_01-Jan-2019.txt Based on the string parameter inserted in: Code:
read -p "Enter starting date" s_date Code:
s_date = 01-Jan-2019 Code:
cat auto_test_01-Jan-2019.txt auto_test_02-Jan-2019.txt auto_test_03-Jan-2019.txt auto_test_04-Jan-2019.txt auto_test_05-Jan-2019.txt auto_test_06-Jan-2019.txt auto_test_07-Jan-2019.txt auto_test_08-Jan-2019.txt auto_test_09-Jan-2019.txt auto_test_10-Jan-2019.txt auto_test_11-Jan-2019.txt auto_test_12-Jan-2019.txt auto_test_13-Jan-2019.txt auto_test_14-Jan-2019.txt auto_test_15-Jan-2019.txt > Period:auto_test$s_date-$e_date.txt Also, I have seen that both of you have used the list option; isn't there a way to skip this part in the scripting area? If I am creating the bash file, it is mandatory to create the list with the all 365 days that are present in the calendar? Isn't there an elegant option to pass the information based on the starting date, ending date and the pattern of the filename? Most likely, the report will be run with the following parameters as dates: Code:
s_date=15-Jan-2019 |
Can be done with one line in bash. Keep it in a file and edit as appropriate.
Code:
cat auto_test_{15..31}-Jan-2019 auto_test_{01..15}-Feb-2019 >> Period |
Hi,
make use of the date command. Code:
#!/bin/bash Evo2. |
Code:
Code:
#!/bin/bash Thanks. One last question though. In the meantime, business has decided that in the future they will be switching the pattern to: auto_test_2019-Jun-15.txt for the files. 1. So, if I am changing the following line: Code:
date=$(date +'%d-%b-%Y' --date "$s_date +$i days") Code:
date=$(date +'%Y-%b-%d' --date "$s_date +$i days") 2. If am switching the s_date and e_date to: Code:
s_date=2019-Jan-15 Code:
date=$(date +'%d-%b-%Y' --date "$s_date +$i days") Code:
date=$(date +'%Y-%b-%d' --date "$s_date +$i days") Code:
date: invalid date ‘2019-Jan-15 +1199 days’ |
Hi,
it seems that '%Y-%b-%d' is not a format the date automatically recognises. So, you'll need to use a different format for loop control to what is used in the file names. Eg. Make the following change in the script: Code:
((i+=1)) Code:
f="auto_test_$(date +'%Y-%b-%d' --date "$s_date +$i days").txt" Evo2. |
Yep, this worked.
Thanks a lot. This can be closed now. |
Just taking what works, and adding more error checking, ease of use to it, and a log.
Code:
#!/bin/bash |
If there is a file each day, you can use simple start and stop matches, if the files are sorted in sequence.
Code:
# example: Code:
ls -tr | Code:
xargs cat |
All times are GMT -5. The time now is 07:35 AM. |