Unix Shell Scripting using Date Command - shell

Ok, so i'm trying to write a scrpit to wc files using the date command. The format of the files, for example, goes like this: testfile20170104.gz.
Now the files are set up to have yesterday's date with the format yyyymmdd. So if today is 1/5/2017 the file will have the previous day of 1/4/2017 in the yyyymmdd format, as you see in the example above.
Normally to count the file all one needs to do is simply input: gzcat testfile20170104.gz|wc -l to get the word count.
However, what I want to do is run a script or even a for loop that gzcat the file but instead of having to copy and paste the filename in the command line, I want to use the date command to input put yesterday's date in the filename with the format of yyyymmdd.
So as a template something like this:
gzcat testfile*.gz|wc -l | date="-1 days"+%Y%m%d
Now I know what I have above is COMPLETELY wrong but you get the picture. I want to replace the '*' with the output from the date command, if that makes sense...
Any help will be much much appreciated!
Thanks!

You want:
filename="testfile$( date -d yesterday +%Y%m%d ).gz"
zcat "$filename"

Related

Shuffle and get ISO date from a file using shuf Unix command

I am trying to fetch the ISO timestamp from a file using shuf command and store the timestamp in a variable.
I have the timestamp in the format date +"%Y-%m-%dT%H:%M:%S:+0000
This timestamp I am storing in a variable using shuf command. However, all I get in the output is date +"%Y-%m-%dT%H:%M:%S:+0000 instead of the actual datetime.
TIMESTAMP=$(shuf -n 1 dateTimestamp.txt)
echo $TIMESTAMP //gives output as date +"%Y-%m-%dT%H:%M:%S:+0000 instead of displaying the values for Y,m,d,H,M,S
dateTimestamp.txt file has below content.
date +"%Y-%m-%dT%H:%M:%S:+0530"
date +"%Y-%m-%dT%H:%M:%S:+0000"
How can I modify to get the correct timestamp fetched from the file using shuf command.
Building on Nejat's comment for the solution, to ensure the source is trusted, you can build the temporary file on the fly and use it as your input. You could place that in either /tmp or in ${HOME}/tmp or anywhere else you may prefer.
#!/bin/sh
INPUT="myShuf$$.txt"
cat >${INPUT} <<-!EnDoFfIlE
date +"%Y-%m-%dT%H:%M:%S:+0530"
date +"%Y-%m-%dT%H:%M:%S:+0000"
!EnDoFfIlE
TIMESTAMP=$($(shuf -n 1 ${INPUT}))
echo $TIMESTAMP

extract datetime format from a file name in unix using shell scripting

In UNIX,The file format will be like
ABCD2013Jan118225516.txt
(date & time is appended with file name). I want the output like
2013-01-01 18:22:55.16
in date and time format.
if the file name is always 4 char then use cut command like
cut -c5-9 file.txt > date.txt
cut -c10-8 file.txt > time.txt
Then you can display the date inside the files
If the filename is not constant use sed command to fetch date and time only.
Thanks

store output of ls - lrt in two different variables

I want to store out put of ls - lrt | tail -2 in two different variables and get the base file name. file name have the pattern YYYYMMDD_filename. I want to compare both the files with current date and pick the previous day file. please help. I m new to shell scripting.
This may not answer your question. To get all the files with yesterday's date:
yesterday=$( date -d yesterday +%Y%m%d )
files=( "$yesterday"_* )
It's generally advised to avoid parsing the output of ls.

Bash Script to convert date format

I'm looking to replace the exiting date format (CCYY-MM-DD) with MM-DD-CCYY date format in a large file using shell script. Before changing the date format, it should look like:
2014-01-31|2014-01-31|
And after change the date format should it look like:
01-31-2014|01-31-2014|
Using sed you can do it:
sed -i.bak -r 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\2-\3-\1/g' file

Find and replace date within a file

My apologies if my title is not descriptive enough, I believe the following will be.
I have 3 files which are just plain text, within each file, is a date
Date: 2012-08-31 for example
I would like to get a command/script to find this and update to the current date, but the date will be ever changing and may not be known going in (without viewing the contents of the file
Knowing what the date is, its simple enough with sed, but how can I do this knowing the syntax of the line I want to modify, but not the specific values. ("Date: " at least is unchanging)
Assuming your date format is unchanging, and all three files are the only three text files in your PWD, you could use GNU sed like this:
sed -r 's/Date: [0-9]{4}-[0-9]{2}-[0-9]{2}/Date: 2012-09-01/g' *.txt
today=`date +%F`
sed -r -i '.bak' "s/Date: [0-9]{4}-[0-9]{2}-[0-9]{2}/Date: $today/g" file1 file2 file3

Resources