Operating System - HP-UX
1827804 Members
2125 Online
109969 Solutions
New Discussion

Re: awk with two variables in sh script

 
Bernd Dittmar
Advisor

awk with two variables in sh script

Must use sh!
As $1 i get a string with variable stringlength.
With awk '{print length($1)}' i want to get the stringlength of $1 in an variable like bl.
This variable has to be used in the script.
How the variable can be used from stdin?

BaaN IV on HP-UX
6 REPLIES 6
Thierry Poels_1
Honored Contributor

Re: awk with two variables in sh script

hi,

echo $1
bl=$(echo $1 | awk "{print length(\"$1\")}")
echo $bl

good luck,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Jean-Louis Phelix
Honored Contributor

Re: awk with two variables in sh script

Hi,

I wouldn't use awk in this case :

#!/usr/bin/sh
L=${#1}
echo "length of '$1' is $L"

would be easier ...

Regards.
It works for me (© Bill McNAMARA ...)
Steve Steel
Honored Contributor

Re: awk with two variables in sh script

Hi

typeset -i length=$(echo $1|wc -c)-1
echo $length


length is then the integer value of the string length of $1


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
John Meissner
Esteemed Contributor

Re: awk with two variables in sh script

Bernd... sounds like homework
and if you NEED to use awk:

string=$1
bl=$(echo $string | awk '{print $1}' | wc -m)
echo $bl

of course this can be done without awk completely

bl=$(echo $1 | wc -m)
All paths lead to destiny
Mike Miller_8
Regular Advisor

Re: awk with two variables in sh script

Try the following . . .

let c=$(echo $1 | wc -m)-1

= Mike =

Jordan Bean
Honored Contributor

Re: awk with two variables in sh script


If you want to work with a list of strings on stdin, then follow this model:

#!/usr/bin/sh
while read string
do
length=${#string}
echo $length $string
done