Tuesday, December 3, 2013

sed notes

I am a sed newb. Today I encountered usages of it that I want to note for future reference.

sed 's/^M//g' SHOP-684.sql > SHOP-684-noM.sql

Note: Hold the control key and then press v and m to get the control-m character.

This removes the ctrl-M's that litter Windows-saved files in a unix env. In a true unix env you can use dos2unix, but on a mac that command (and its counterpart unix2dos) are unavailable.

In order to replace ^M with newlines on a Mac, I had to do:

$ sed 's/^M/\    [ HIT ENTER ]
--- /g' SHOP-684.sql > SHOP-684-noM.sql

I have a giant file (almost 1 million lines) that I need to edit to remove tabs and other detritus to convert it into a usable SQL file. Opening it in intelliJ or vi takes a while, so it is great to be able to do this instead. A big plus: it returns almost immediately. It's very fast.

In order to replace tabs, since the version of sed on a Mac does not support \t in the left side of "s///", I used the control for it, which happens to be ^I. It looks like this when you hold the control key and press v and then i to get the control-i character:

sed "s/      //g" SHOP-684-noM.sql > SHOP-684-noT.sql

See also: