I have a set of .csv files (all in one folder) with the format shown below:
170;151;104;137;190;125;170;108
195;192;164;195;171;121;133;104
... (a lot more rows) ...
The thing is I screwed up a bit and it should look like this
170;151;104.137;190.125;170;108
195;192;164.195;171.121;133;104
In case the difference is too subtle to notice:
I need to write a script that changes every third and fifth semicolon into a period in every row in efery file in that folder.
My research indicate that I have to devise some clever sed s/ command in my script. The problem is I’m not very good with regular expressions. From reading the tutorial it’s probably gonna involve something with /3 and /5.
Here’s a really short way to do it:
It replaces the 3rd and then the 5th (which is now the 4th) instances of the
;with..I tested it on your sample (saved as sample.txt):
For safety, I have made my example back up your originals as
<WHATEVER>.BAK. To prevent this, change-iBAKto-i.This script may not be totally portable but I’ve tested it on Mac 10.8 with BSD sed (no idea what version) and Linux with sed (gsed) 4.1.4 (2003). @JonathanLeffler notes that it’s standard POSIX
sedas of 2008. I also just found it and like it a lot.Golf tip: If you run the command from bash, you can use brace expansion to achieve a supremely short version: