Operating System - HP-UX
1834149 Members
3917 Online
110064 Solutions
New Discussion

Need some shell script advise about preserving blank spaces

 
SOLVED
Go to solution
Debbie Fleith
Regular Advisor

Need some shell script advise about preserving blank spaces

I need to set a variable for use is a shell script to the last line of a text file that contains lots of extra blank spaces. For example, the file's last line would be something like: "Mary had a little lamb".

I need to preserve these blank spaces. I can run the following command at a command line:

# tail -1 FILENAME
and get the correct result: "Mary had a little lamb"

However, when I try to move the contents into a shell script variable, it removes all extra blank spaces.
# A=`tail -1 FILENAME`
# echo $A
my results are:
"Mary had a little lamb"

Does anyone know a work around for this?
7 REPLIES 7
Joseph C. Denman
Honored Contributor

Re: Need some shell script advise about preserving blank spaces

I don't see any difference?? Where are the spaces? At the end? beginning? middle?

Could you please post exactly what you want and what you are getting?

Thanks,

...jcd...
If I had only read the instructions first??
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Need some shell script advise about preserving blank spaces

Hi Debbie:

Your method should work.

Try this to make sure as you echo your output:
echo "A = \"${A}\""
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Need some shell script advise about preserving blank spaces

Debbie,

I am not sure if I understood your question correctly. I couldn't see any blank spaces anywhere. I would do the following.

cat file |sed 's/ /#/g' > /tmp/file$$
A=`tail -1 /tmp/file$$ |sed 's/#/ /g'`

I am not sure if this helps.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
John Poff
Honored Contributor

Re: Need some shell script advise about preserving blank spaces

Hello,

I played with a sample file and an little script, and I was able to reproduce your problem. I hacked on it a little bit and I was able to get a shell script to read all of the line into a variable, including spaces. The trick was to set the IFS variable to null. That variable contains the characters that separate a word, and normally the space character is a word separator. By setting it to null, the shell thinks that the entire line is a single word and puts it all into the variable.

It looks like this:

fduxdd01:/home/users/jpoff/junk>cat mary.txt
Here is
some text
Mary had a little lamb
fduxdd01:/home/users/jpoff/junk>cat marytest.sh
#!/bin/sh

# marytest.sh

tail -1 mary.txt | wc

IFS="" MYLINE=$(tail -1 mary.txt)

echo $MYLINE
echo $MYLINE | wc

fduxdd01:/home/users/jpoff/junk>./marytest.sh
1 5 29
Mary had a little lamb
1 5 29


There is probably a neater way to do it, but this is my hack at it. I hope this helps.

JP
John Poff
Honored Contributor

Re: Need some shell script advise about preserving blank spaces

Oops. In my last reply, I forgot to mention that I put six spaces at the end of the last line in the mary.txt file. The 'wc' command shows 29 characters in that line, which includes the spaces. If you take out the IFS="" part of the line in the marytest.sh script, you'll see that 'wc' returns only 23 characters.

JP
Jordan Bean
Honored Contributor

Re: Need some shell script advise about preserving blank spaces

echo consolidates whitespaces between parameters, so `echo $A` will reduce the output while `echo "$A"` will preserve the spaces.
Wodisch
Honored Contributor

Re: Need some shell script advise about preserving blank spaces

Hello Debbie,

you could set your $IFS to only tabs and then use only
tabs in your command line - that way spaces are *real*
text and not delimiters...

HTH,
Wodisch