• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

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.
0
scpig
Asked:
scpig
  • 9
  • 4
  • 4
  • +2
2 Solutions
 
PapertripCommented:
sed 's#$test#<b>$test</b>#g'
0
 
PapertripCommented:
FYI it was garbled because you didn't escape $
0
 
scpigAuthor Commented:
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:

0
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
PapertripCommented:
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

0
 
scpigAuthor Commented:
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?
0
 
PapertripCommented:
sed 's/\$test/<b>\$test<\/b>/g'

Open in new window

0
 
TintinCommented:
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"
0
 
PapertripCommented:
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).
0
 
PapertripCommented:
# 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.
0
 
PapertripCommented:
$ 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




0
 
PapertripCommented:
@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


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

Open in new window

0
 
scpigAuthor Commented:
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

0
 
AnacreoCommented:
Try this:

sed -e "s#$test#<b>&</b>#g"
0
 
AnacreoCommented:
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.
0
 
AnacreoCommented:
Here is the output and test:
 
118 ~/bin >ksh
# help=me
# echo "Help me with this" | sed -e "s+$help+<b>&</b>+g"
Help <b>me</b> with this

Open in new window

0
 
farzanjCommented:
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.
0
 
AnacreoCommented:
Not sure exactly what you're looking for but this may give you a finer amount of control... for instance you could further refine the ORA errors by groupings...  This will also have the bad/positive feature of throwing out white space in the formatting and return one line per Ora Error, all white space will be converted to a space...

#!/bin/ksh

# Put field 6 into our oraerror variable
oraerror=`cut -d'|' -f6 test.txt`

# Load the oraerror as our command arguments
set - $oraerror

# Rotate through these...
while [ "$1" ]; do
  case $1 in
        # Found all ORA error match strings
        ORA-[0-9]*)
                # Echo a new line if we need too...
                [ "$error" ] && echo
                echo -n "<b>$1</b>"
                error=true
        ;;
        *)
                # Don't send output unless we've already seen an Ora Error
                [ "$error" ] && echo -n " $1"
        ;;
  esac
  shift
done

# Throw one extra new line to finish our last ora message.
[ "$error" ] && echo

Open in new window

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 9
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now