Unix Shell - use of SED in script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2006 > use of SED in script





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author use of SED in script
Obelix

2006-12-19, 7:32 am

Hy, all

I want to substitute a string with another, but that something I do that
is wrong.

A=`ls ../staz`;

CTR1="m_"
CTR2=".png"

if [ "$A" != "" ]; then

for ITEM in $A; do

#cerco il ctr

`ls ../staz/$ITEM"/"$CTR1$ITEM$CTR2 2>/dev/null 1>/dev/null`

if [ "$?" = "2" ]; then

sed -e 's/<ctr>staz\/$ITEM\/m_$ITEM.png<\/ctr>/<ctr><\/ctr>/g'
tmp.xml > tmp1.xml

#sed -e 's/<ctr>staz\/203\/m_203.png<\/ctr>/<ctr><\/ctr>/g' tmp.xml >
tmp1.xml

fi

done
fi

If I use: sed -e 's/<ctr>staz\/203\/m_203.png<\/ctr>/<ctr><\/ctr>/g'
tmp.xml > tmp1.xml

there is no problem.
But, if I use
sed -e 's/<ctr>staz\/$ITEM\/m_$ITEM.png<\/ctr>/<ctr><\/ctr>/g' tmp.xml >
tmp1.xml

The result is not what I want.

anyone can help me ?

Regards,
Obelix.
Barry Margolin

2006-12-19, 1:22 pm

In article <em8hvt$9p7$1@mophus.csi.it>, Obelix <Obelix@asterix.ga>
wrote:

> Hy, all
>
> I want to substitute a string with another, but that something I do that
> is wrong.
>
> A=`ls ../staz`;
>
> CTR1="m_"
> CTR2=".png"
>
> if [ "$A" != "" ]; then
>
> for ITEM in $A; do
>
> #cerco il ctr
>
> `ls ../staz/$ITEM"/"$CTR1$ITEM$CTR2 2>/dev/null 1>/dev/null`


You don't need the backticks here. Backticks are used when you want to
substitute the output of a command into another command.

You also don't need double quotes around the / character, you should put
them around the variables. So it should be:

ls "../staz/$ITEM/$CTR1$ITEM$CTR2" 2>/dev/null 1>/dev/null

>
> if [ "$?" = "2" ]; then
>
> sed -e 's/<ctr>staz\/$ITEM\/m_$ITEM.png<\/ctr>/<ctr><\/ctr>/g'
> tmp.xml > tmp1.xml
>
> #sed -e 's/<ctr>staz\/203\/m_203.png<\/ctr>/<ctr><\/ctr>/g' tmp.xml >
> tmp1.xml
>
> fi
>
> done
> fi
>
> If I use: sed -e 's/<ctr>staz\/203\/m_203.png<\/ctr>/<ctr><\/ctr>/g'
> tmp.xml > tmp1.xml
>
> there is no problem.
> But, if I use
> sed -e 's/<ctr>staz\/$ITEM\/m_$ITEM.png<\/ctr>/<ctr><\/ctr>/g' tmp.xml >
> tmp1.xml
>
> The result is not what I want.
>
> anyone can help me ?


Variables are not expanded inside single quotes, use double quotes
instead.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
Michael Tosch

2006-12-21, 1:20 pm

Barry Margolin wrote:[vbcol=seagreen]
> In article <em8hvt$9p7$1@mophus.csi.it>, Obelix <Obelix@asterix.ga>
> wrote:
>
>
> You don't need the backticks here. Backticks are used when you want to
> substitute the output of a command into another command.
>
> You also don't need double quotes around the / character, you should put
> them around the variables. So it should be:
>
> ls "../staz/$ITEM/$CTR1$ITEM$CTR2" 2>/dev/null 1>/dev/null
>

Maybe the ls and the if can be replaced by just one if, testing
for a regular file:

if [ -f "../staz/$ITEM/$CTR1$ITEM$CTR2" ]; then



--
Michael Tosch @ hp : com
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com