LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-15-2010, 11:55 AM   #1
dukedog
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Rep: Reputation: 0
printf white space issue word splitting


I am having trouble keeping the name together and the phone number together, I think due to the white space. I have tryed "" and '' it doesn' seem to matter. So it may be my syntax? and does it matter how long the first and last names are.


me$ echo 'fstname lstname' '123 123-1234' | ./myscript


#myscript
read a b
printf "%-15s %20s\n" $a $b >> my_phone_numbers

OUTPUT

fstname lstname
123 123-1234
insted of
fstname lstname 123 123-1234

i know its not an elegant script but im still learning how some commands work

thanks to the people who help on this site it has helped many times in the past
 
Old 06-15-2010, 12:36 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
The need for quoting on the command line is a separate issue from the word splitting done inside the script. They're necessary to keep the input strings together when sending the values to the script, but they're not part of the input itself.

Inside the script you have to reapply the quotes wherever you need the spaces to be seen as part of the string, around variable references in particular.
Code:
read a b
printf "%-15s %20s\n" "$a" "$b" >> my_phone_numbers
Edit: I just noticed that there's a second issue here as well. The quotes in the input line only protect the input long enough to pass the values to echo; then it's echo that pipes them into the script. This means that the spaces are NOT protected when they hit read. You can check it yourself:
Code:
read a b
echo "a is $a"
echo "b is $b"
printf "%-15s %20s\n" "$a" "$b" >> my_phone_numbers
...and the output:
Code:
$ echo 'fstname lstname' '123 123-1234' | ./myscript
a is fstname
b is lstname 123 123-1234
firstname       lastname 123 123-1234
The only way I know to overcome that, as long as you use echo and a pipe, is to escape the spaces in order to protect them from echo.
Code:
$ echo -e "fstname\ lstname" "123\ 123-1234" | ./myscript
a is fstname lstname
b is 123 123-1234
firstname lastname         123 123-1234

Last edited by David the H.; 06-15-2010 at 01:12 PM.
 
1 members found this post helpful.
Old 06-15-2010, 01:46 PM   #3
dukedog
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs up

thanks again for all the help, That did the trick
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash: why is word splitting not effective in an assignment catkin Programming 5 12-12-2009 12:02 PM
LXer: White Space LXer Syndicated Linux News 0 09-22-2007 08:40 PM
m4 and white space, does it exist? The_Nerd Programming 3 04-18-2007 05:19 PM
removing white space accent11 Linux - Software 4 10-06-2004 01:30 AM
Two Tuxes with block of white space??? robbow52 Debian 1 07-04-2004 01:05 PM


All times are GMT -5. The time now is 04:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration