Retain Consecutive Spaces

March 3, 2010 at 04:06:42
Specs: Windows XP
Using Korn, when I READ a line, consecutive spaces are replaced by a single space. Can this be avoided?

See More: Retain Consecutive Spaces

Report •

#1
March 3, 2010 at 13:59:55
Could you post how you are reading the line and a sample of your data file?

Report •

#2
March 4, 2010 at 08:02:19
#!/bin/ksh

fname=$1
mydelim=$2
myposition=$3

echo "fname is [$fname]"
echo "mydelim is [$mydelim]"
echo "myposition is [$myposition]"

# create dynamic variables for each position
indcnt=0
for i in $(echo ${myposition})
do
((indcnt+=1))
eval a$indcnt=\$i
done
while read mystring
do
newstring=""
count=0
length=$(expr "$mystring" : '.*')
until [ $count -eq $length ]
do
((count+=1))
char=$(echo $mystring|cut -b"$count")
newstring=${newstring}${char}
ncnt=0
# cycle thru the dynamic variables
until [ $ncnt -eq $indcnt ]
do
((ncnt+=1))
val=$(eval echo \"\$a$ncnt\")
if [[ $count -eq $val ]]
then
newstring=${newstring}${mydelim}
# stop if one is found
break
fi
done
done
echo $newstring
done < $fname


Report •

#3
March 4, 2010 at 11:15:35
I looked at your script, but without knowing what your data looks like or even what your goal is, I think the leading and trailing spaces are being affected? is that correct?

The problem area is:

while read mystring

I take it the $mystring variable is the problem?


Anyway, I do have a suggestion; whenever your data contains whitespace, I would surround my variables with double quotes. For example:

char=$(echo $mystring|cut -b"$count")

should be:

char=$(echo "$mystring" |cut -b"$count")

AND:

echo $newstring

should be:

echo "$newstring"


Report •
Related Solutions


Ask Question