Yoe Pass Yoe Pass - 6 months ago 10
Bash Question

Looping process by using AWK bash

I try to do like the output below by using AWK but still have problem in looping process.

# # # r e a d //'r' is the focus character, before 'r' is null & assign with "#" and after 'r' are e,a,d
# # r e a d # //'e' is the focus character, before 'e' is r and after 'e' are a,d
# r e a d # # //'a' is the focus character, before 'a' are r,e and after 'a' are d,i,n,g
r e a d # # # //'d' is the focus character, before 'd' are r,e,a and after 'd' is null and sign with "#"


Here is the way I'm coding:

awk '
{
for (i=1; i<=length($1); i++) print "#"
for (j=1; j<=1; j++) print substr ($1, j, 1)
print ""
} ' file


But only first filed is appear and how to make the next field looping? Please guide me

#
#
#
r

Answer

my wild guess is you're trying something like this

$ echo "hello" | awk '{word=$1; len=length(word); tlen=2*len-1;
                       for(i=1;i< len;i++) a[i]="#"; 
                       for(j=1;j<=len;j++) a[i+j-1]=substr(word,j,1);
                       for(r=1;r<=len;r++) 
                         {for(i=1;i<=tlen;i++) printf "%s ", a[(i+r-2)%tlen+1];
                          print "" }}'

# # # # h e l l o
# # # h e l l o #
# # h e l l o # #
# h e l l o # # #
h e l l o # # # #

I think it can be simplified little more but not sure it's correct or not.

Comments