Bash script - mass modify files sed regular expression

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.

Answers


Here's a really short way to do it:

sed 's/;/./3;s/;/./4' -iBAK *

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):

$ sed 's/;/./3;s/;/./4' <sample.txt
170;151;104.137;190.125;170;108
195;192;164.195;171.121;133;104

For safety, I have made my example back up your originals as <WHATEVER>.BAK. To prevent this, change -iBAK to -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 sed as 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:

sed -es/\;/./{3,4} -i *

Need Your Help

Can't get “Sign In” link to work in Hartl's Rails Tutorial. Getting: No route matches “/signin_path”

ruby-on-rails railstutorial.org

As the title says, I'm getting an the following error when clicking the "sign in" link when running my localhost app: No route matches [GET] "/signin_path"

Deserialize Json Object - DateTime

c# wpf web-services microsoft-metro

My web-api returns an User Object. In that object there is a DateTime property. When i'am reading it in my Application i get an error because the string that would represent the DateTime isn't vali...