aliascshsetenv

How do I simultaneously replace ":" and ";" with a newline?


Background

The tr command translates, delete, or squeezes characters from standard input. I am trying to create a cshell alias that outputs the setenv command such that each file or directory in the path is displayed on a new line.

In other words, I want this example output of setenv:

PATH=/some/dir:/some/second/dir:/some/third/dir

PROFILES= file:///some/file/here.txt;file:///some/second/file.xml;file:///some/third/file/there.pl

To become this:

PATH= /some/dir

/some/second/dir

/some/third/dir

PROFILES= file:///some/file/here.txt

file:///some/second/file.xml

file:///some/third/file/there.pl

Related Aliases that work

I have created aliases that output the individual environment variables like above:

  1. For PATH: alias readablePath = 'echo "$PATH" | tr : '\'\\\n\'' '
  2. For PROFILES: alias readableProfiles= 'echo "$PROFILES" | tr \; 'echo "$PATH" | tr : '\'\\\n\'' '

Attempted aliases that do not work

These are the aliases that I have tried that return tr: no match.

  1. alias readEnv 'echo "setenv" | tr [:\;] '\'\\\n\'' '
  2. alias readEnv 'echo setenv | tr [:\;] '\'\\\n\'' '
  3. alias readEnv 'echo setenv | tr [:\\;] '\'\\\n\'' '
  4. alias readEnv 'echo setenv | tr [:;] '\'\\\n\'' '

These are the aliases that I have tried that return the string "setenv".

  1. alias readEnv 'echo setenv | tr "[:;]" '\'\\\n\'' '
  2. alias readEnv 'echo "setenv" | tr "[:;]" '\'\\\n\'' '

Question

How do I create an alias that successfully takes the output of setenv and inserts a newline where there is a ":" or a ";"?


Solution

  • To replace multiple characters in your (unix/linux) command line alias, you should rather use sed and regular expressions, as in Search and replace with sed when dots and underscores are present .
    So something like:

    sed 's/\.\|,/\\n/g'
    

    where \. finds a dot (needs to be escaped, else it finds any character), \| is the or operation, also escaped, and the new line also must have its \\ escaped.