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.
The way I did is:
sed -e "s/$test/<b>$test<\/b>/g"
but I got this error: sed: command garbled
Please help. Thanks.
sed 's#$test#<b>$test</b>#g'
FYI it was garbled because you didn't escape $
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:
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>
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?
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'
The reason it didn't work for you is that Papertrip used single quotes instead of double quotes.
Should be
or you can use any seperator you like, eg:
sed "s_$test_<b>$test</b>_g"
Should be
sed "s#$test#<b>$test</b>#g"
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).
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>
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
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>
Solaris:$ cat kshsed
#!/usr/bin/ksh
sed 's#$test#<b>$test</b>#g' /tmp/tt
$ ./kshsed
<b>$test</b>
@scpig
Can you run this quick script? Just curious.
Can you run this quick script? Just curious.
#!/bin/ksh
sed 's#$test#<b>$test</b>#g' /tmp/file
[root@broken ~]# cat /tmp/file
$test
Oh update the hashbang for wherever ksh is on your system.
which ksh
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-1 0|Failed|O RA-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
>/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-1
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"
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.