LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sed command - command garbled errror (https://www.linuxquestions.org/questions/linux-newbie-8/sed-command-command-garbled-errror-937315/)

gauavmahesh 03-30-2012 04:01 PM

Sed command - command garbled errror
 
I am a lerner to sed.Was just trying to run the following command on solaris:

echo "NotWorking" |sed s/[a-z]/\(&\)/

Basically want to add to every char with '( )' but i get command garbled error.


Need help.
Thanks

smallpond 03-30-2012 04:18 PM

Not sure about Solaris, but on Linux you need '-e' before your substitution to tell it to execute the following script.

Dark_Helmet 03-30-2012 08:02 PM

Quote:

Originally Posted by smallpond
Not sure about Solaris, but on Linux you need '-e' before your substitution to tell it to execute the following script.

You can use -e, but it's not a requirement. I find that it can help to break up multiple, independent patterns with multiple -e options just to make things easier to understand, but for quickie one-liners, I rarely use the -e switch.

Also, I'm not too familiar with Solaris. My main suggestion though is you should enclose your sed script in quotes. This prevents the shell from looking at your script and trying to find job control/wildcard characters to interpret or expand. For bash, the '&' character (among others) is a job control character meaning to send the command to the background. Using quotes also saves you from a lot of backslash-escaping.

For instance:
Code:

user@localhost$ echo "NotWorking" | sed 's/[a-z]/(&)/'
N(o)tWorking

As you can see, the sed substitution takes place only once. Your original goal is to add the parentheses around each (lowercase?) character, then something like:
Code:

user@localhost$ echo "NotWorking" | sed 's/[a-z]/(&)/g'
N(o)(t)W(o)(r)(k)(i)(n)(g)

Lastly, if you truly wanted parentheses around every character:
Code:

user@localhost$ echo "NotWorking" | sed 's/[a-zA-Z]/(&)/g'
(N)(o)(t)(W)(o)(r)(k)(i)(n)(g)


gauavmahesh 03-30-2012 10:04 PM

Thanks smallpod and Dark_Helmet - It did worked on Linux but the same command did not work on solaris.Thanks i could try it on my home linux machine.


All times are GMT -5. The time now is 07:17 AM.