Removing leading spaces from the variable value.

Hi All,

I am trying to replace the value of a xml tag with a new one. But, the existing value in the xml contain leading spaces and I tried to remove that with different sed commands but all in vain.

For replacing the value I wrote the command in BOLD letters below:

bash-3.00$ polnum=`grep "<PolNumber>" adi99.xml | sed 's:<PolNumber>::;s:</PolNumber>::' | sed 's/^ *\(.*\) *$/\1/'`
bash-3.00$ echo "PolNumber is:"$polnum
PolNumber is: 109301793
newpol=101101
bash-3.00$ echo "cat adi99.xml | sed -e 's/${polnum}/${newpol}/'"

cat adi99.xml | sed -e 's/"THERE ARE SPACES WHICH ARE GETTING REMOVED HERE ONCE I SAVE IT HERE" 109301793/101101/'

cat adi99.xml
<Holding id="Holding_1">
<Policy id="Policy_1">
<PolNumber>109301793</PolNumber>
</Policy>
</Holding>

Can you please suggest a way to remove these spaces.

bash might help you---

BASH:~> blah='  1234567'
BASH:~> echo "$blah"
  1234567
BASH:~> echo "${blah/ /}"
 1234567
BASH:~> echo "${blah// /}"
1234567
BASH:~>

${blah/ /} This will remove only the first space...
${blah// /} this will remove all spaces.

Hi khedu,

I think you have remove all whitespace characters instead of just regular spaces, so instead of:

polnum=`grep "<PolNumber>" adi99.xml | sed 's:<PolNumber>::;s:</PolNumber>::' | sed 's/^ *\(.*\) *$/\1/'`

try:

polnum=`grep "<PolNumber>" adi99.xml  | sed 's:<PolNumber>::;s:</PolNumber>::'| sed 's/^[[:space:]]*\(.*\)[[:space:]]*$/\1/'`

or

polnum=`grep "<PolNumber>" adi99.xml  | sed 's:<PolNumber>[[:space:]]*\(.*\)[[:space:]]*</PolNumber>:\1:'`

or

polnum=$(sed -n 's:<PolNumber>[[:space:]]*\(.*\)[[:space:]]*</PolNumber>:\1:p adi99.xml)

no more grep, nor uuoc, nor extra spaces

$ polnum=$(sed -n '\:<PolNumber>:s:<PolNumber>\([^<]*\)</PolNumber>:\1:p' adi99.xml)
$ echo "PolNumber is:"$polnum
PolNumber is: 109301793
$ newpol=101101
$ sed 's/'"$polnum"'/'"$newpol"'/' adi99.xml
<Holding id="Holding_1">
<Policy id="Policy_1">
<PolNumber>101101</PolNumber>
</Policy>
</Holding>