How to change date format with Sed command? - shell

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

Related

Use sed to replace regex with a variable

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.

How to find lines with current date with %d-%h-%Y format assigned to a variable in all files

I have a directory under which i have many access files like:
access
access87681
access98709
Now i am trying grep all those lines with current date in format +%d-%h-%Y.
I have written like below:
tm1=$(date '+%d-%h-%Y')
sed -n '/$tm1/p' $dir/access* > $loc/OUD_Req_Res_matrix_data
I am trying to grep all $dir/access* files with $tm1 which is current date in above date format and pushing them into output file $loc/OUD_Req_Res_matrix_data.
the above code is not working. Please suggest
With tm1=$(date '+%d-%h-%Y') and access-files without spaces, you can use:
sed -n "/${tm1}/p" $dir/access*
When the date has slashes, the sed command would break.
You can escape the slashes with
sed 's#/#\\/#g' <<< "$tm1"
Use this in your command:
sed -n "/$(sed 's#/#\\/#g' <<< "$tm1")/p" $dir/access*

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 Find & Replace Issue

I have a pretty huge .csv file with the date at column 3 ( Example: 11/17/2015) and i need to replace with the date format as 2015-11-17. I tried doing using:%s/\<11/17/2015\>/2015-11-17.But couldn't see the change. Any suggestions on how to do this?.
I assume you are using vim:
:%s/11\/17\/2015/2015-11-17/g
You can do this also with sed without opening the file:
sed -i 's/11\/17\/2015/2015-11-17/' somefile.csv
Try to escape backslash like:
echo "11/17/2015" | sed 's/11\/17\/2015/2015-11-17/g'
2015-11-17

Issue with reformatting a date using sed

I'm working on a shell script to reformat a CSV file exported from Access into a format that can be imported more easily into MySQL.
There's a number of different operations I need to perform on the file, and I'm currently stuck on one of them. I've used sed and awk a bit before, but I'm not great with them (I'm used to PCRE), and I'm at a loss to figure out where I've gone wrong here.
The command I've written is as follows:
sed -e '1d' raw.csv | sed 's/£//g' | sed 's/ 00:00:00//g' | sed 's/\([0-9]{2}\)\/\([0-9]{2}\)\/\([0-9]{2}\)/20\3-\1-\2/g' > formatted.csv
Now, the operations I carry out here are as follows:
Delete the first line
Remove all pound signs
Remove all unwanted instances of 00:00:00
Reformat the date from mm/dd/yy to yyyy-mm-dd
I've worked my way through these in order and they work as expected, except for the last one:
sed 's/\([0-9]{2}\)\/\([0-9]{2}\)\/\([0-9]{2}\)/20\3-\1-\2/g' > formatted.csv
Can anyone see where I've gone astray?
You'll need to escape the curly brackets too.
sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{2\}\)/20\3-\1-\2/g' > formatted.csv

Resources