Monday, January 12, 2015

Selected differences in sed on mac and other *nix

A coworker sent me this sed command that works in other versions of unix command line:

sed -i "s/filename_prd/filename_dev/g" filename_dev.sql

But on Mac OS this what I had to do:

sed 's/filename_prd/filename_dev/g' ./filename_prd.sql > ./filename_dev.sql

Evidently the "-i" is not required in Mac, the command being sent to sed must be in single quotes and not double, and the output of sed goes to std_out so it needs to be saved to another file. (You can save it to the file itself but I prefer to be conservative about this.)

Thursday, October 30, 2014

Thursday, October 2, 2014

grep in a bunch of files

I always have to look this up so I'm posting a note to myself here. To grep through all the files in a directory, recursively, and only get back a list of the files containing the string, do

$ grep -lr "searchterm" location

E.g.:

$ grep -lr "Enum" /var/log/rtr/pops

$ grep -lr "receipt" .

Found this in this blog.

Friday, September 5, 2014

Avoid error messages with `find` in *nix

Very useful comment on StackOverflow about how to get rid of error messages that come up when you use `find`.
You do not need sudo to run find for generally-accessible commands. If you don't want to see the error messages about inaccessible directories, get rid of the messages rather than using root privs unnecessarily. Using sudo all the time is a bad habit. Redirect stderr to /dev/null, like this:
find / -name java 2> /dev/null

Walter Underwood on SO

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:



Wednesday, July 24, 2013

IFTTT test post!

I created a simple IFTTT recipe to sms me if there's a new post on this blog. Testing it out now.

This links (for me) to my personal IFTTT recipes: https://ifttt.com/myrecipes/personal

IFTTT stands for If This Then That.

Monday, April 22, 2013

How to find out if a domain name is a CNAME in Unix/Linux

$ host -t cname qadbrw01 
qadbrw01.cluster is an alias for va-qa-dbrw101.cluster.