for example this string: gLZMQp8i
Loop become easy if we add space between each char, How to do it?
or other solutions are welcome.
for example this string: gLZMQp8i
Loop become easy if we add space between each char, How to do it?
or other solutions are welcome.
What you want to do in the loop?
try..
echo 'gLZMQp8i' | sed 's|.|&\n|g'
I have found this code....
var="example text";var1=$(echo $var | wc -c);for((i=$var1;i>=0;i--));do echo $var | cut -c $i;done
it worked great, Could you explain the usage of &
I found it
/regexp(3,n)/replacement/
Attempt to match regexp(3,n) against the pattern space. If success-
ful, replace that portion matched with replacement. The
replacement may contain the special character & to refer to that
portion of the pattern space which matched, and the special
escapes \1 through \9 to refer to the corresponding matching
sub-expressions in(1,8) the regexp(3,n).
---------- Post updated at 09:34 PM ---------- Previous update was at 09:28 PM ----------
I want to count the totoal number of each class a-z, A-Z and 0-9.
Is there better way than loop?
A possible solution :
$ cat count.sh
$ cat c.sh
eval $( echo "$*" | \
awk '
{ l += gsub(/[a-z]/, "");
u += gsub(/[A-Z]/, "");
d += gsub(/[0-9]/, ""); }
END {
printf("lower_cnt=%d;upper_cnt=%d;digit_cnt=%d\n",
l, u, d) } '
)
echo lower=$lower_cnt
echo upper=$upper_cnt
echo digit=$digit_cnt
$ count.sh Happy New Year 2010
lower=9
upper=3
digit=4
$
Jean-Pierre.
I have tried this so...
echo 'gLZMQp8i' | sed 's/\(.\)/\1 /g'
result:
g L Z M Q p 8 i
I also found the interesting fold command
> echo 'gLZMQp8i' | fold -w1
g
L
Z
M
Q
p
8
i
---------- Post updated at 10:18 PM ---------- Previous update was at 10:12 PM ----------
Thanks, there must be simple way to do it.
expr is promising, but i can't get it work
expr "gLZMQp8i" : '[0-9]'
Now the quetion is how to sum the the number of each class?
a-z, A-Z and 0-9
---------- Post updated at 10:35 PM ---------- Previous update was at 10:18 PM ----------
That is what i have worked out so far.
var=gLZMQp8i
var2=`echo $var| sed 's/[A-Z]//g'`
total_upper= $((${#var}-${#var2}))
.....
total_upper=$(echo "$var" | awk '{print gsub(/[A-Z]/,"")}')
Jean-Pierre.
how about something along lines of:
# echo 'gLZMQp8i' | fold -w1 | sed 's/[a-z]/%/;s/[A-Z]/^/;s/[0-9]/=/' | sort |uniq -c | sort -nr | tr "^%=" "Ul#"
4 U
3 l
1 #
i.e. 4 upper (U), 3 lower(l) and 1 number (#)
HTH