Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2007
    Posts
    14

    Unanswered: 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

  2. #2
    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

  3. #3
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

  4. #4
    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

  5. #5
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    There should be 11 spaces right before the symbol =
    Perhaps you should of stated this originally.

    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"

  6. #6
    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.

  7. #7
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

  8. #8
    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 19:03.

  9. #9
    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.

  10. #10
    Join Date
    Jun 2007
    Location
    London
    Posts
    2,527
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •