Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script - mass modify files sed regular expression

Tags:

regex

bash

sed

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.

like image 868
Krzysztof Jarzyna Avatar asked Sep 09 '12 00:09

Krzysztof Jarzyna


2 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 *
like image 100
Alex Brown Avatar answered Nov 14 '22 02:11

Alex Brown


Here's one way:

sed -i 's/^\([^;]*;[^;]*;[^;]*\);\([^;]*;[^;]*\);/\1.\2./' foldername/*

(Disclaimer: I did test this, but some details of sed are not fully portable. I don't think there's anything non-portable in the above, so it should be fine, but please make a backup copy of your folder first, before running the above. Just in case.)

like image 1
ruakh Avatar answered Nov 14 '22 02:11

ruakh