Operating System - HP-UX
1833767 Members
2218 Online
110063 Solutions
New Discussion

Scripting question: retaining spaces when looping read/echo statements

 
SOLVED
Go to solution
John J Read
Frequent Advisor

Scripting question: retaining spaces when looping read/echo statements

Hoping someone can help. I am attempting to write a simple loop that will echo or print the following 3 fields. The problem I am having is that "echo" and "print" both seem to treat consecutive spaces as one space. I'm used to using awk -F" " but as you can see the first field sometimes has spaces which is why I'm trying to use cut with character lengths.



Input file contains:
GA30494 0000 Y john_j_read@aul.com
GA620870220022 Y john_j_read@aul.com

My loop:
cat extfile |while read line
do
CONTRACT=`echo $line |cut -c 1-20`
ACT=`echo $line |cut -c 21`
SENDTO=`echo $line |cut -c 22-82`
echo "${CONTRACT}" "${ACT}" "${SENDTO}"
done
GA30494 0000 Y john_ j _read@aul.com
GA620870220022 Y joh n _j_read@aul.com


Notice that "cat" and cut are not the problem:

#cat extfile |cut -c 1-20
GA30494 0000
GA620870220022

#cat extfile |cut -c 21
Y
Y

#cat extfile |cut -c 22-82
john_j_read@aul.com
john_j_read@aul.com


Thanks! I'm willing to entertain other language options such as Perl!
5 REPLIES 5
James Specht
Trusted Contributor
Solution

Re: Scripting question: retaining spaces when looping read/echo statements

change your echo to printf

printf "%s20 %1s %60s\n",$CONTRACT,$ACT,$SENDTO

Should do the trick.

--Jim
"Everyone can be taught to sculpt: Michelangelo would have had to be taught how not to. So it is with the great programmers."
John J Read
Frequent Advisor

Re: Scripting question: retaining spaces when looping read/echo statements

Dag nabit! It looks like this forum page also treats consective spaces as one space. It looked good when I cut/pasted it in but is collapsed it.

In the above post, the input file should look like:

Field 1 is colums 1-20
Field 2 is column 21
Field 3 is colums 22-82

GA30494 0000 Y john_j_read@aul.com
GA620870220022 Y john_j_read@aul.com



Dietmar Konermann
Honored Contributor

Re: Scripting question: retaining spaces when looping read/echo statements

Try to pass the strings as ONE arg to echo. E.g. echo "$line" with quotes.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
John J Read
Frequent Advisor

Re: Scripting question: retaining spaces when looping read/echo statements

Thanks! I was working out the new printf lines when I saw Dietmar's post.

I'll give you both 10's!

Looks like putting quotes around $line is the easiest answer for me!

It worked! Thanks very much!!!

cat extfile |while read line
do
CONTRACT=`echo "$line" |cut -c 1-20`
ACT=`echo "$line" |cut -c 21`
SENDTO=`echo "$line" |cut -c 22-82`

echo "${CONTRACT}" "${ACT}" "${SENDTO}"
done
Eric Buckner
Regular Advisor

Re: Scripting question: retaining spaces when looping read/echo statements

It looks like you have a good solution. I just wanted to add that you might try to typeset your variables as well.

typeset -L20 CONTRACT
typeset -L60 SENDTO

That would left justify and pad on the right w/ whitespace out to 20 or 60 respectively. There is also the R equivalent as well.

Time is not a test of the truth.