Link to home
Start Free TrialLog in
Avatar of scpig
scpig

asked on

UNIX Shell "sed"

I have a variable that I would like to add <b> in the front and </b> at the end.
The way I did is:

sed -e "s/$test/<b>$test<\/b>/g"

but I got this error: sed: command garbled

Please help.  Thanks.
Avatar of Papertrip
Papertrip
Flag of United States of America image

sed 's#$test#<b>$test</b>#g'
FYI it was garbled because you didn't escape $
Avatar of scpig
scpig

ASKER

when I tried it, it was just freeze there.  no error and no return output.

FYI:
when I echo $test, it returns two values:
echo $test
ORA-12096: ORA-01653:

That was just the corrected sed switch syntax to fix your garbled error as you asked.  In what context you are using the switch I don't know, but it is valid overall.

[root@broken ~]# cat qqq
$test
[root@broken ~]# sed 's#$test#<b>$test</b>#g' qqq
<b>$test</b>

Open in new window

Avatar of scpig

ASKER

My machine is SunOS  Generic_144488-17 sun4u sparc SUNW,Sun-Fire
The command does not work in this machine.  And my script is Korn shell script.
Is there a way to do to fit this machine?  backslash instead of # sign?
sed 's/\$test/<b>\$test<\/b>/g'

Open in new window

Avatar of Tintin
The reason it didn't work for you is that Papertrip used single quotes instead of double quotes.

Should be

sed "s#$test#<b>$test</b>#g"

Open in new window


or you can use any seperator you like, eg:

sed "s_$test_<b>$test</b>_g"
Is that because he is using ksh?

BTW Tintin it was you who showed me the glory of not having "leaning toothpick" syndrome, or whatever the clever term was that you used :)  Thanks again for that, I never needed to use more than a few escapes before (don't do heavy scripting).
# echo $0
ksh
# sed 's#$test#<b>$test</b>#g' qqq
<b>$test</b>

Open in new window


Oh well, if double quotes works for the asker then good deal.
$ sed 's#$test#<b>$test</b>#g' tt
<b>$test</b>
$ uname -a
SunOS phxdns1 5.10 Generic_118822-26 sun4v sparc SUNW,Sun-Fire-T1000
$ echo $0
ksh

Open in new window


Just in case somehow this was an issue between command line and script -

Linux:
[root@broken ~]# cat kshsed
#!/bin/ksh
sed 's#$test#<b>$test</b>#g' /tmp/qqq
[root@broken ~]# ./kshsed
<b>$test</b>

Open in new window

Solaris:
$ cat kshsed
#!/usr/bin/ksh
sed 's#$test#<b>$test</b>#g' /tmp/tt
$ ./kshsed
<b>$test</b>

Open in new window




@scpig

Can you run this quick script?  Just curious.
#!/bin/ksh
sed 's#$test#<b>$test</b>#g' /tmp/file

Open in new window

[root@broken ~]# cat /tmp/file
$test

Open in new window


Oh update the hashbang for wherever ksh is on your system.
which ksh

Open in new window

Avatar of scpig

ASKER

which ksh
>/usr/bin/ksh

This is what I tested to retrieve ORA- errors and add <b></b>:

in test.txt file:
Shedule Job,Daily Schedule Job|Data Load-IL|28-SEP-10|28-SEP-10|Failed|ORA-12096: error in view log on T_RATE_PLAN" ORA-01653: unable to extend table MLOG$_T_RATE_PLAN by 1024 in tablespace SYSTEM|

test=`cut -d"|" -f6 test.txt | awk -- '{for (i=1; i<=NF; i++) if ($i ~ /ORA-[0-9]*/) print $i}'`
> echo $test
>ORA-12096: ORA-01653:
> sed "s#$test#<b>$test</b>#g"
sed: command garbled: s#ORA-12096:
 > sed "s_$test_<b>$test</b>_g"
ksh: test_: parameter not set

It seems none of them works for me.  
Besides, after cut, two Oracle errors seem become one string.  How do I separate them to get this result?  <b>ORA-12096:</b>  <b>ORA-01653:</b>

Thanks

Try this:

sed -e "s#$test#<b>&</b>#g"
Sorry on my previous post:

You're missing the -e in the SED command, also & will replace the matched string in the replace text...

For instance:
sed -e 's/.*/"&"/g'

Will wrap the entire line in double quotes.
ASKER CERTIFIED SOLUTION
Avatar of Anacreo
Anacreo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sed -e "s/$test/<b>$test<\/b>/g"

TRY THIS:

sed "s/$test/<b>&<\/b>/g" filename

if you want it to be modified at the same time, use it with option -i but you have to have a fairly recent version of it.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of scpig

ASKER

All of these comments do not really resolve my issue.  However, it does help to improve my knowledge, so I still appreciate the helps.