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

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

Related

Unix Shell Scripting using Date Command

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"

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

BASH scripting: get pattern from command output, serve it as a variable to another command for processing [...]

I have files with data in this format:
2014-02-05 09:43:30;data;data;data;data;data;
2014-02-05 09:52:10;data;data;data;data;data;
2014-02-05 10:02:41;data;data;data;data;data;
This data is already processed by searching for specific "data" patterns with a grep | awk syntax and produces an output in the same format as the original file data. This output is displayed on the screen.
What I'm interested in is the first part:
2014-02-05 09:43:30
The server I'm running this on has the date in GMT (UTC) timezone and the data is also in this format. I want to take the above portion of data and substitute it like this:
remove 2014-02-05 09:43:30 from the generated output
feed the time part (09:43:30) to TZ=America/Washington date -d "09:43:30" (as a saved variable for each processed line) in order to get the output of date/time for the specified timezone (America/Washington)
reinsert the output of TZ=America/Washington date -d "09:43:30" into the original output (generated by the grep | awk), replacing the original date and processed time with the new date and time generated by the TZ=America/Washington date -d "09:43:30"
re-display the modified output
I am having issues with the syntax logic behind the process I want to implement, as described above in the bullets.
Try something like
#!/bin/bash
while read -r line; do
first="${line%%;*}"
second="${line#*;}"
export TZ=EST
echo $(date -d "$first GMT" '+%Y-%m-%d %H:%M:%S')";$second"
done < <(grep)
Example output
> abovescript
2014-02-05 04:43:30;data;data;data;data;data;
2014-02-05 04:52:10;data;data;data;data;data;
2014-02-05 05:02:41;data;data;data;data;data;

Resources