Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spaces in parameter

Status
Not open for further replies.

Waspy

Vendor
Aug 4, 2003
32
GB
There're many questions of stripping leading/trailing spaces. My problem is the opposite. I would like to retain any spaces in the field! awk command seems to strip off all except for 1 space.

e.g. input == '20AA12 ZZ','20BB1 ZZ'
FS == ',' (comma)
Item == fix length of 10 char with embedded spaces.

printf ("'%s'\n",$1) and printf ("'%s'\n",$2) will result in '20AA12 ZZ' and '20BB1 ZZ'
i.e the item ended up with 9 and 8 chars respectively!

How to I preserve the extra spaces in the the items?
Thanks.
 
use " instead of ' by imput string. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
the double quote doesn't seems work.
forgot to mention that the input is read from an external file.

e.g. fname == itemlst.dat

'20AA1...ZZ','20BB1...ZZ'
'30AA12..ZZ','30BB12..ZZ'
'40AA123.ZZ','40BB123.ZZ'
'50AA1234ZZ','50BB1234ZZ'

where the dot respresents the number of spaces.

e.g. of the script file (test.sh)

####################################
cat "./itemlist.dat" |while read ilist
do
item2=`echo ${ilist} | awk'
BEGIN{
FS=","
}
{
printf("'%s'\n",substr($2,2,8))
}' `

#some actions to work with the var item2
#e.g.
echo [${item2}]
done
####################################


instead of the expected output of :

[20BB1...]
[30BB12..]
[40BB123.]
[50BB1234]

the script output is :

[20BB1.ZZ]
[30BB12.Z]
[40BB123.]
[50BB1234]

each dot represent a space.
tested with both single/dbl quotes
 
echo "aaa bbb,qq -e "s/[^,]*/'&'/g"|tr ',' '\n'
gives you
'aaa bbb'
'qq www'
analog
sed -e "s/[^,]*/'&'/g" filename |tr ',' '\n'
------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top