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.
Shell Scripting

Avatar of undefined
Last Comment
scpig
Avatar of Papertrip
Papertrip
Flag of United States of America image

sed 's#$test#<b>$test</b>#g'
Avatar of Papertrip
Papertrip
Flag of United States of America image

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:

Avatar of Papertrip
Papertrip
Flag of United States of America image

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
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?
Avatar of Papertrip
Papertrip
Flag of United States of America image

sed 's/\$test/<b>\$test<\/b>/g'

Open in new window

Avatar of Tintin
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"
Avatar of Papertrip
Papertrip
Flag of United States of America image

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).
Avatar of Papertrip
Papertrip
Flag of United States of America image

# 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.
Avatar of Papertrip
Papertrip
Flag of United States of America image

$ 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




Avatar of Papertrip
Papertrip
Flag of United States of America image

@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


Avatar of Papertrip
Papertrip
Flag of United States of America image

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

Open in new window

Avatar of scpig
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

Avatar of Anacreo
Anacreo

Try this:

sed -e "s#$test#<b>&</b>#g"
Avatar of Anacreo
Anacreo

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of farzanj
farzanj
Flag of Canada image

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
Avatar of Anacreo
Anacreo

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of scpig
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.
Shell Scripting
Shell Scripting

The term 'shell' refers to a general class of text-based command interpreters most often associated with the UNIX and Linux operating systems. Popular shells include Bourne, Debian Almquist (dash), Korn (ksh), Bourne Again (bash) and the C shell family (csh). Some view the DOS 'cmd' prompt as a minimal shell of sorts. It is also possible to install Cygwin on Windows and emulate a full Unix environment with complete shell capabilities. Terminal emulators, such as xterm, GNOME Terminal and OS X Terminal, can be used to access shell.

11K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo