If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Why r mu spaces missing

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-26-07, 11:03
atl_geek atl_geek is offline
Registered User
 
Join Date: Jul 2007
Posts: 14
Why r mu spaces missing

I have a following script

i=4
temp='Rows Read = '$i
echo $temp


Iam getting the following output
Rows Read = 4

I understand why my spaces are missing. Any clues be appreciated

thx
Tony
Reply With Quote
  #2 (permalink)  
Old 07-26-07, 11:28
atl_geek atl_geek is offline
Registered User
 
Join Date: Jul 2007
Posts: 14
Plz ignore my typos in my previous question

Following is the problem

i=4
temp='Rows Read = '+$i

when i say echo $temp iam getting the following output

Rows Read = 4

I see my spaces have been removed right before =

Any one has a clue why korn shell behave this way
Reply With Quote
  #3 (permalink)  
Old 07-26-07, 12:45
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
I don't have ksh on my machine and your code works fine in bourne shell but try:

Code:
i=4
echo "Rows Read = $i"
Mike
Reply With Quote
  #4 (permalink)  
Old 07-26-07, 13:28
atl_geek atl_geek is offline
Registered User
 
Join Date: Jul 2007
Posts: 14
There should be 11 spaces right before the symbol =
For some reason those 11 spaces are disappearing
i=4
temp ='Rows Read = '$i

For some reason those 11 spaces are disappearing
Reply With Quote
  #5 (permalink)  
Old 07-26-07, 16:19
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
There should be 11 spaces right before the symbol =
Perhaps you should of stated this originally.

Quote:
For some reason those 11 spaces are disappearing
Are you sure the problem is in your shell script - I ran my bit of code above (with 11 spaces) and it shows them fine. Could you try producing a very small shell script just to test these lines only? Is something else processing the output of this script and removing the 11 spaces?
Code:
#!/bin/sh

i=4
echo "num rows           = $i"
If that doesn't work then just avoid using spaces ie
Code:
#!/bin/sh

i=4
echo "--------- Num rows = $i"
Reply With Quote
  #6 (permalink)  
Old 07-26-07, 17:18
atl_geek atl_geek is offline
Registered User
 
Join Date: Jul 2007
Posts: 14
echo command works fine. But when you try to send a string with leading or trailing spaces then they are disappearing. I cannot use any other charecter other than space as iam using this code snippet to grep a certain char set which has space.
Reply With Quote
  #7 (permalink)  
Old 07-26-07, 18:57
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
The code I gave you with the spaces before the = sign works fine and prints all the spaces that you wanted. If something else is removing them then I'd look there for your problem.

As another alternative could you just change the grep command to look for just the "Rows Read" rather than the whole string?

Mike
Reply With Quote
  #8 (permalink)  
Old 07-27-07, 17:12
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Try:
Code:
printf "Rows read           = %d" "$i"
instead of echo for displaying.
If you want to 'grep' for a string containing 11 spaces you could also:
Code:
egrep "Rows read {11}= $i"
Regards

Last edited by Tyveleyn; 07-27-07 at 18:03.
Reply With Quote
  #9 (permalink)  
Old 07-27-07, 18:03
atl_geek atl_geek is offline
Registered User
 
Join Date: Jul 2007
Posts: 14
When I use echo or printf it will work fine. But i need to save it in a variable. When i save it a variable , it is replacing all the consecutive spaces with one space.

I need to save it as a string in a variable so that i can use it in my grep command when i do a pattern search.
Reply With Quote
  #10 (permalink)  
Old 07-28-07, 08:23
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
The simplest solution is just to change the grep to:
Code:
# even simpler might be just to use
grep "Rows Read"
Or, if this might match the wrong rows, use a more complete match like:
Code:
# match against Rows Read followed by any number of spaces then
# an = sign and any number of spaces and then a number
grep "Rows Read *= *[0-9][0-9]*"
No doubt, in time, you'll give us enough information to answer your query fully

Mike
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On