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
Related
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
From the Unix command line, how do I convert a date string in the format MMDDYYYY to the format YYYY-MM-DD? For instance, how do I convert 02032017 to 2017-02-03? Thanks!
I have tried this, but I'm afraid it did not work.
date -d "02032017"
This produces this error message.
date: invalid date ‘02032017’
String manipulation with variable expansions
input_date=02032017
output_date=${input_date:4}-${input_date::2}-${input_date:2:2}
This should work,
DATE="02032017"; date -d"$DATE" +%Y-%m-%d
Error in the above answer.
Try this one
month=$(cut -c 1-2)
date=$(cut -c 3-4)
year=$(cut -c 5-8)
date=$year-$month-$date
echo $date
You can use a date library that allows you to specify the incoming date's format, for example perl's Time::Piece:
input_date=02032017
perl -MTime::Piece -sE 'say Time::Piece->strptime($date, "%m%d%Y")->ymd' -- -date="$input_date"
2017-02-03
I'm trying to create a bash script where I can replace a date in a filename with the current date, however, I'm not being able to do so.
Here's what I have so far:
#!/bin/bash
my_file="FILENAME_20170410235908_GTT_DEV_20170410235400_20170410235408_XX_YY.nc"
my_date=`date "+%Y%m%d"`
echo "$my_file" | sed 's/\([0-9]\{12\}\)/"${my_date}"/g'
I'm currently getting this:
FILENAME_"${my_date}"08_GTT_DEV_"${my_date}"00_"${my_date}"08_XX_YY.nc
Howerver, this is what I'd like to have:
FILENAME_2019070135908_GTT_DEV_20190701235400_20190701235408_XX_YY.nc
How can I achieve this?
You can try
sed "s/\([0-9]\+\)/${my_date}/g"
single quote will not replace variable data.
my_date will have only date. If you want the timestamp also, add it from the 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"
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