Echo working funny

Hello, this is my first post and question. I have search before for this problem but didn't find anything similar.

My case: I have a string inside the variable string1 like this:

string1="lala lele lili lolo lulu"

When I do echo of it, it appears like this:

echo $string1
lala lele lili lolo lulu

But if I do echo with characters before and after, it appears like this:

echo "123 $string1 123"
123 lala
lele
lili
lolo
lulu 123

What's the problem?

NOTE: I have generated string1 with an AWK loop, but I have already test it with this example without AWK and the same happends!

Help me please!

Regards.

Quote the variable:

echo "123 "$string1" 123"

Regards

It DOES NOT happend if you do it like in my example.

I think is something related with AWK. Is it inserting carriage returns between words? This is my AWK code.

It's runned in HP-UX. swList contains a list of packages and the comand swlist gives you the same list, but with 3 or more colums:

  • The first one the name of the pacjage binari.
  • The second one the version.
  • The third one and the rest the package full name.
for package in $swList; do

    # Package name
    package_name=$(swlist | grep "$package" | awk '{
    
        word_count=3
        
        while (word_count <= NF) {
            
            print $ word_count
            
             word_count++
            
        }
    }')
    
    # Package version
    package_version=$(swlist | grep "$package" | awk '{ print $2 }')
    
    echo "software^^^$oa_package_name^^^$oa_package_version^^^"

done

Help!

---------- Post updated at 04:49 AM ---------- Previous update was at 04:45 AM ----------

Thanks!

But it does not work with the string I made using that AWK code...

And there is a mistake in the code I put in my previous post. The last code line is:

echo "software^^^" "$package_name" "^^^" "$package_version" "^^^"

done

Just in case someone thought that was the problem :wink:

Regards

---------- Post updated at 05:04 AM ---------- Previous update was at 04:49 AM ----------

Well, I don't know why it happens but I have solved the problem doing the echo like this:

echo "software^^^"$(echo $oa_package_name | tr "\n" " ")"^^^$oa_package_version^^^ ^^^ ^^^ ^^^^^^ ^^^"

tr converts the carriage return into a space character.

Regards.

This actually unquotes the variable!

echo "123 "$string1" 123"

Can you post the whole script and state you Operating System and Shell?

Use printf in your awk code instead of a print statement, replace this:

print $word_count

with:

printf "%s " $word_count

hehe, yes! It does unquote the variable, because of that I put a pipe after the AWK loop like this:

        word_count++
            
        }
    }' | tr "\n" " ")

This worked fine!

HP-UX, KSH. The script is... too long to be posted! hehehe

Thank you very much anyway ;)[COLOR="\#738fbf"]

---------- Post updated at 05:41 AM ---------- Previous update was at 05:36 AM ----------

Didn't work, got this message in return:

awk: There are not enough parameters in printf statement %s sudo.
 The input line number is 1.
 The source line number is 7.

Trying to find the solution... this looks fancier than pipe the exit to tr statement :wink:

Regards

Not sure what you were trying to achieve here but beware that "swlist" on its own lists the bundles. Each bundle can contain multiple packages.

your print statement in the awk is culprit...when you do

print $word_count

it prints $word_count , each in a new line
instead use

One more thing from your while loop

You should have told more clearly what you wanted from this while loop...
may be this...

$(word_count)=word_count

in this case use print in the

END {print}.

What we are doing is a software audit of a HP-UX system. Do you think that is better to use

swlist -l product

for example? We have to compare it with the licenses purchased and we thought that it would be easier to compare with the bundles. Because that's the way the software is purchased, isn't it?

Thanks again!

---------- Post updated at 08:20 AM ---------- Previous update was at 08:16 AM ----------

(code parts ripped by me)

I did this while loop because I need to print all the columns beginning in the 3rd one till the end of the line, from a text with multiple lines.

Why to do it like that? Because the name of the software products have multiple words, and they are different from one to another, so I use NF to reach the end of the line!

And, of course, because I don't know any better way to do it! I'm a newbie!

Thanks for your suggestions!

---------- Post updated at 09:24 AM ---------- Previous update was at 08:20 AM ----------

Again, I will answer myself:

It's better to use swlist -l product

This will list all the software installed.

Another thing, about the AWK while loop I put there, for better functionality I have added another pipe to head -1, to take just the first coincidence of the string.

The problem was that some software names where also "inside" other software names, for example:

Program1
Program12

When I do grep 'Program1', I get both as the result. If somebody is thinking about some parameter, HP-UX is SO limited about it, if you compare it with Linux for instance.

With grep 'Program1' | head -1 I take just the first result, which is the one I want!

Thank you all.