Use sed to replace regex with a variable - bash

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.

Related

How to change date format with Sed command?

I am trying to change the date format from YYYY-MM-DD to DD/MM/YYYY in all lines of a file using the sed command.
For example:
2021-04-01 00:15,69,0,38,1,1,0,0,0,32
should be:
01-04-2021 00:15,69,0,38,1,1,0,0,0,32
I have tried the following using regular expressions:
sed -E 's,[0-9]{4}-[0-9]{2}-[0-9]{2},\3-\2-\1,g'
unfortunately this does not work and gives me an error (not defined in the RE).
it would be really great if someone could help me to solve this.
You forgot capturing groups with ( ):
$ sed -E 's,^([0-9]{4})-([0-9]{2})-([0-9]{2}),\3-\2-\1,g' <<< '2021-04-01 00:15,69,0,38,1,1,0,0,0,32'
01-04-2021 00:15,69,0,38,1,1,0,0,0,32

Replace a date in file to another date using sed not workiing

I need to replace a date in file on device to another date . But the following sed command for string replace is not working .
path= "/var/local"
last_date=d("cat /var/local”)-----06/24/18 date i
previous_date=(datetime.strptime(last_date, '%m/%d/%y')-timedelta(1)).strftime("%m/%d/%y”)--output --06/23/18
"sed -i 's/%s/%s/g' %s" % (last_date, previous_date, file)
This Sed give output as 24/24/18. It is not replacing the complete date
It looks like your problem is that you're effectively running the following command:
sed -i "s/06/24/18/06/23/18/g" /var/local
As you can see, the problem is that you're not escaping the slashes in your regex.
The shortest fix would be to call .replace("/", "\\/") on both last_date and previous_date in your python code.
However, since you're already using Python, you might do better to only use pure Python, and not use sed at all.

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"

Unix: Removing date from a string in single command

For satisfying a legacy code i had to add date to a filename like shown below(its definitely needed and cannot modify legacy code :( ). But i need to remove the date within the same command without going to a new line. this command is read from a text file so i should do this within the single command.
$((echo "$file_name".`date +%Y%m%d`| sed 's/^prefix_//')
so here i am removing the prefix from filename and adding a date appended to filename. i also do want to remove the date which i added. for ex: prefix_filename.txt or prefix_filename.zip should give me as below.
Expected output:
filename.txt
filename.zip
Current output:
filename.txt.20161002
filename.zip.20161002
Assumming all the files are formatted as filename.ext.date, You can pipe the output to 'cut' command and get only the 1st and 2nd fields :
~> X=filename.txt.20161002
~> echo $X | cut -d"." -f1,2
filename.txt
I am not sure that I understand your question correctly, but perhaps this does what you want:
$((echo "$file_name".`date +%Y%m%d`| sed -e 's/^prefix_//' -e 's/\.[^.]*$//')
Sample input:
cat sample
prefix_original.txt.log.tgz.10032016
prefix_original.txt.log.10032016
prefix_original.txt.10032016
prefix_one.txt.10032016
prefix.txt.10032016
prefix.10032016
grep from start of the string till a literal dot "." followed by digit.
grep -oP '^.*(?=\.\d)' sample
prefix_original.txt.log.tgz
prefix_original.txt.log
prefix_original.txt
prefix_one.txt
prefix.txt
prefix
perhaps, following should be used:
grep -oP '^.*(?=\.\d)|^.*$' sample
If I understand your question correctly, you want to remove the date part from a variable, AND you already know from the context that the variable DOES contain a date part and that this part comes after the last period in the name.
In this case, the question boils down to removing the last period and what comes after.
This can be done (Posix shell, bash, zsh, ksh) by
filename_without=${filename_with%.*}
assuming that filename_with contains the filename which has the date part in the end.
% cat example
filename.txt.20161002
filename.zip.20161002
% cat example | sed "s/.[0-9]*$//g"
filename.txt
filename.zip
%

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

Resources