SED command line utility – split string (& grep)

SED is a useful Unix command line tool I use for simple text replacement.

Here’s an example to see the directories included in your Windows PATH environment variable.  (I use MinGW for a Unix-like Shell in Windows)

echo $PATH

displays all directories on one line separated by semicolons.

With sed, we can list each directory on its own line.

echo $PATH | sed 's/:/\n/g'

First we echo $PATH, then pipe the output to the sed command to do our string replacement. The /g modifier will substitute ALL matches, not just the first.

Now each directory is listed on a separate line.

Additionally, if you are looking for a specific directory, for example you want to see which SVN installation is included in your PATH, you can use the grep command.

echo $PATH | sed -e 's/:/\n/g' | grep -i 'svn'

Once again we pipe the output from sed and use grep to show only directories that include SVN. The -i option is to ignore case (if your folder name includes the text “Svn”, “SVN”, etc.)


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s