Bash Script-Pattern Printing Scripts

If you give the following pattern in a file  'test'

A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y 
 


Pattern 1:



A B C D E
F G H I
K L M
P Q
U

code

file=$1
awk 'BEGIN { a=0;}
{for(i=1;i<=NF-a;i++){printf("%s ",$i);if(i==NF-a)printf("\n")}a+=1;}
END {print "Check the pattern"}' $file


copy the above code in file pattern1.sh  then,
 
# chmod 755 pattern2.sh
# ./pattern1.sh test 


 
 
Pattern 2:

 A
  G
    M
      S
        Y
 
 code

file=$1
awk 'BEGIN { c=1;}
{ for (i=1; i<=c; i++) printf("  "); printf("%c\n",$c); c++; }
END {print "Check the pattern"}' $file


copy the above code in file pattern2.sh  then,
 
# chmod 755 pattern2.sh
# ./pattern1.sh test 
 
 
Pattern 3: 

          E
        I
      M
    Q
  U


code


file=$1
awk 'BEGIN { a=0;}
{for(i=1;i<=NF-a;i++){printf("  ");if(i==NF-a)printf("%c\n",$i)}a+=1;}
END {print "Check the pattern"}' $file

copy the above code in file pattern1.sh  then,
 
# chmod 755 pattern3.sh
# ./pattern3.sh test 
 
                      or
 
file=$1
awk 'BEGIN { a=0;}
{for(i=NF;i>=1+a;i--) printf("  "); print substr($(NF-a),1,1); a++;}
END {print "Check the pattern"}' $file
 

copy the above code in file pattern1.sh then,

# chmod 755 pattern3.sh

# ./pattern3.sh test
 
 
 
 


This entry was posted by Unknown. Bookmark the permalink.