Unix Find & Replace Issue - shell

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

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.

Bash Script how to use sed to replace text after a number?

{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]}
Im stuck on how I would use sed to remove anything after 66719222 including and then adding ]} at the end. The output should be
This is what I want to delete:
,{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]}
and replace it with ]}
so the output is
{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"}]}
Thanks if anyone could help.
sed 's/,{"Id":66719222.*/]}/' filename
Or if you're willing to sacrifice clarity to make the command one character shorter:
sed 's/,[^}]*66719222.*/]}/' filename
Or more crudely:
sed 's/.\{7\}66719222.*/]}/' filename
Assuming file is your filename.
sed 's/\(.*\),{"Id":66719222.*/\1\]}/' file
If it is in bash, why not just use bash:
str='{"success":true,"message":"","result":[{"Id":66719299,"TimeStamp":"2017-08-29T16:35:35.937","Quantity":0.80122000,"Price":0.01379000,"Total":0.01104882,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719283,"TimeStamp":"2017-08-29T16:35:32.36","Quantity":7.36427025,"Price":0.01379000,"Total":0.10155328,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719222,"TimeStamp":"2017-08-29T16:35:13.263","Quantity":24.03098850,"Price":0.01379000,"Total":0.33138733,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719221,"TimeStamp":"2017-08-29T16:35:13.013","Quantity":0.70000000,"Price":0.01379000,"Total":0.00965300,"FillType":"PARTIAL_FILL","OrderType":"SELL"},{"Id":66719220,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":31.30579055,"Price":0.01379000,"Total":0.43170685,"FillType":"PARTIAL_FILL","OrderType":"BUY"},{"Id":66719219,"TimeStamp":"2017-08-29T16:35:12.623","Quantity":12.87144703,"Price":0.01378000,"Total":0.17736854,"FillType":"PARTIAL_FILL","OrderType":"BUY"}]}'
echo "${x%66719222*}66719222]}"

Remove leading Zero in CSV file using Unix sed command

My csv file is:
1234,0045,0045,0345,300
1235,0046,0446,2345,301
1236,0047,0447,0350,302
I need the result like this:
1234,45,45,345,300
1235,46,446,2345,301
1236,47,447,350,302
I want to accomplish this task using Unix SED command. Any other alternative method is also appreciated.
Try this :
$ sed 's/^0*\([^,]\)/\1/;s/,0*\([^,]\)/,\1/g' yourfile.csv
1234,45,45,345,300
1235,46,446,2345,301
1236,47,447,350,302

Removing Unicode Line Separator "U+2028" in Bash

I have a text file with a unicode line separator (hex code 2028).
I want to remove it using bash (I see implementations for Python, but not for this language). What command could I use to transform the text file (output4.txt) to lose the unicode line separator?
See in vim below:
Probably this tr command should also work:
tr '\xE2\x80\xA8' ' ' < inFile > outFIle
Working solution: Thanks to OP for finding this:
sed -i.old $'s/\xE2\x80\xA8/ /g' inFile
I noticed that in your screenshot, you have already opened file in vim, then why not just do the substitution in vim?
in vim you could do
:%s/(seebelow)//g
the (seebelow) part, you could type:
ctrl-vu2028
You can probably use sed:
sed 's/\x20\x28//g' <file_in.txt >file_out.txt
To overwrite the original file:
sed -i 's/\x20\x28//g' file.txt
Edit: (See chepner's comment) You should make sure that you have the correct bytes, depending on the encoding, and then use sed to delete them. You could use e.g. od -t x1 for looking at the hex dump and figuring out the encoding.
This worked for me
sed $'s/\u2028//g' file_in.txt > file_out.txt
Note: other questions use the term <U+2028>

Resources