The following example shows how to align to the right depending on a value of a variable (variable x in this example). x=-10 basicsys@mars~/lec11>awk '{printf "year %'$x'd no\n",$1}' F3 year 1999 no year 200000 no basicsys@mars~/lec11>cat F1 ab cd ef gh ab cd d e f g h basicsys@mars~/lec11>awk 'NF>=3 {print "3",$0} NF==5 {print "5",$0}' F1 3 ab cd ef gh 3 d e f g h 5 d e f g h basicsys@mars~/lec11>echo "ab cd" ef | awk '{print $2}' cd basicsys@mars~/lec11>echo ab cd ef | awk '{print $2}' cd basicsys@mars~/lec11>x=5.5 basicsys@mars~/lec11>y=3.2 basicsys@mars~/lec11>echo $[$x+$y] -bash: 5.5+3.2: syntax error: invalid arithmet basicsys@mars~/lec11>echo $x $y | awk '{print $1+$2}' 8.7 basicsys@mars~/lec11>cat F1 10 20 30 40 45 3.2 5.5 6.3 basicsys@mars~/lec11>cat B1 {print hi} {print $1+$2} {print $1*$2} basicsys@mars~/lec11>awk -f B1 F1 30 200 85 1800 8.7 17.6 basicsys@mars~/lec11>cat B1 {print "hi"} {print $1+$2} {print $1*$2} basicsys@mars~/lec11>cat F1 10 20 30 40 45 3.2 5.5 6.3 basicsys@mars~/lec11>awk -f B1 F1 hi 30 200 hi 85 1800 hi 8.7 17.6 basicsys@mars~/lec11>cat B1 #!/bin/awk -f {print "hi"} {print $1+$2} {print $1*$2} basicsys@mars~/lec11>chmod u+x B1 basicsys@mars~/lec11>B1 F1 hi 30 200 hi 85 1800 hi 8.7 17.6 basicsys@mars~/lec11>cat B1 BEGIN {print "hi"} {print $1+$2} {print $1*$2} BEGIN {print "hello"} END {print "NR="NR} BEGIN {print "ok"} END {print "END"} basicsys@mars~/lec11>cat F1 10 20 30 40 45 3.2 5.5 6.3 basicsys@mars~/lec11>awk -f B1 F1 hi hello ok 30 200 85 1800 8.7 17.6 NR=3 END basicsys@mars~/lec11>cat F1 a b c de fg basicsys@mars~/lec11>cat P1 { nc = nc + length($0) + 1 nw = nw + NF } END {print NR, "lines", nw, "words", nc, "chars"} basicsys@mars~/lec11>awk -f P1 F1 3 lines 5 words 12 chars basicsys@mars~/lec11/examples>cat F1 abc def ghe a b c d basicsys@mars~/lec11/examples>cat F2 100 20000 25 basicsys@mars~/lec11/examples>awk '{print FILENAME,$0}' F1 F2 F1 abc def F1 ghe F1 a b c d F2 100 F2 20000 F2 25 basicsys@mars~/lec11/examples>cat F1 abc def ghe a b c d basicsys@mars~/lec11/examples>cat F2 100 20000 25 basicsys@mars~/lec11/examples>cat F3 abc basicsys@mars~/lec11/examples>cat A1 BEGIN { for (i=0; icat F1 abc de a x basicsys@mars~/lec11>awk -f P1 F1 3 lines 4 words 11 chars basicsys@mars~/lec11>cat F1 abc de a x basicsys@mars~/lec11>cat F2 abdd e f basicsys@mars~/lec11>awk -f P1 F1 F2 5 lines 7 words 20 chars basicsys@mars~/lec11>cat P2 BEGIN {i=1} { if (FILENAME != ARGV[i]) { print ARGV[i], nc, "chars" nc=0; i++ } } { nc = nc + length($0) + 1 } END { print ARGV[i], nc, "chars" } basicsys@mars~/lec11>cat F1 abc de a x basicsys@mars~/lec11>cat F2 abdd e f basicsys@mars~/lec11>awk -f P2 F1 F2 F1 11 chars F2 9 chars