I am running a sql session within a shell script. Later I am performing some validations on the value coming from the sql session.
The problem is that, the value I am getting from sql session have an extra white space at the begining(I am actually, assigning the outcome of the sql session to a variable).
I have tried with sed 's/^ *//', but was unable to delete that white space.
Please let me know why this is happening and the remedy to it.
one way:
sed 's/^ //'
second way:
change the select statement of sql to :
select '~'||column1||'~'
in the shell script :
sed 's/~//g'
Here is the script :
cntrct_desc=`sqlplus -s <<eof
whenever sqlerror exit 1
set feedback off;
set verify off;
set echo off;
set heading off;
select '~'||b.cpgrp_desc||'~' from cont a,cpgrp b
where cpgrp_cont_id_alias='756274934'
and a.cont_num=b.cont_num;
eof`
echo $cntrct_desc
leng=${#cntrct_desc}
echo $leng
cntrct_desc=$(echo "$cntrct_desc" | sed 's/~//g')
echo "contarct description : "123${cntrct_desc}123
The Outcome should be "Linc Children's Hospital".....but its coming as " Linc Children's Hospital"
strange, but the same work's with me.
$ echo " Linc Children's Hospital" | sed 's/^ //'
Linc Children's Hospital
The problem was in the portion I marked in red :
cntrct_desc=`sqlplus -s <<eof
whenever sqlerror exit 1
set feedback off;
set verify off;
set echo off;
set heading off;
select '~'||b.cpgrp_desc||'~' from cont a,cpgrp b
where cpgrp_cont_id_alias='756274934'
and a.cont_num=b.cont_num;
eof`
echo $cntrct_desc
leng=${#cntrct_desc}
echo $leng
cntrct_desc=$(echo "$cntrct_desc" | sed 's/~//g')
echo "contarct description : "123${cntrct_desc}123
That should be....
cntrct_desc=$(echo $cntrct_desc | sed 's/~//g')
Actually double quotes were creating the error.
Thanks a lot again.
Strange... why not just fetch the trimmed value of column "cpgrp_desc" instead of adding those characters and then removing them with sed and all that jazz ??
select TRIM(b.cpgrp_desc) from cont a,cpgrp b
where cpgrp_cont_id_alias='756274934'
and a.cont_num=b.cont_num;
tyler_durden