ANSI code has always been pretty much of a minority interest and
(slightly old fashioned) black art, but it is useful & can be
very effective in conjunction with Awk. This FAQ just covers the
graphics parameters that allow coloured text, reverse video etc -
it does not cover the parameters which control the cursor movement
and on screen placement.
ANSI graphics parameters take the form
ESC[att;attm
where 'ESC' is the escape, 'att' represents one or more attributes
separated by ';' and 'm' terminates the code. In Linux (Unix) the
escape is \033, so:
echo -e '\033[5;41;1;37m *** STOP *** \033[0m'
will print the flashing message ' *** STOP *** ' in bright white
letters on a red background. The instruction '\033[0m' turns off all
attributes and returns the screen to its normal appearance.
A nice feature is that ANSI code is recognized by LESS. ie:
Piping the output of the sample script provided with this FAQ
thru' LESS with either the -r or -R option set:
awk -f awkdir | less -R
will give you the output in colour & with the various LESS functions
available.
There are 8 background colours and the use of the bold attribute provides
16 foreground (the old CGA colour set). The 'concealed on' attribute can
be used to hide input for passwords etc.
Foreground Background
30 - black 34 - blue 40 - black 44 - blue
31 - red 35 - magenta 41 - red 45 - magenta
32 - green 36 - cyan 42 - green 46 - cyan
33 - yellow 37 - white 43 - yellow 47 - white
0 - all attributes off
1 - bold (bright) on
2 - faint on (bold off)
5 - blink on
7 - reverse video
8 - concealed on
The script I've used here to demonstrate the use of ANSI with Awk, runs
'ls -AlF' thru' getline & produces a report at the end (a la DOS) giving
the total size of the directory, number of files etc. The report is
printed in bright white on white (or gray). The directory meanwhile
retains its 'colourized' form with executables printed in bright green,
directories in bright blue, links in yellow & (as an added bonus) backup
files in bright magenta. The colourization, of course, would normally be
lost in the process of running the data thru' Awk - and the backup files
would only ever be identified by the trailing tilde (~).
# NB When using printf() (or sprintf()) set attribs separately
# from the formatting eg:
# printf(" \033[47;1;37m"); printf("%-19s", "No. of files")
# This example:
# printf("%-20s"," \033[47;1;37mNo. of files"
# does not work well (concatenates the expression, the
# formatting is lost).
# Examples of this are shown at 'else if (i == 3) {' (below)
# & also (although less obviously) in the construct:
# printf(" "on)
# printf(spc1, msg[1])
# at the head of the message writing block (below).
# END of TIP---------------------------------------------------
if (i == 1) {
msg[1] = " No. of files"
msg[2] = (dcnt - sdcnt) # all in dir less sub-dirs
msg[3] = (dsz - sdsz) # all size less sub-dir size
}
else if (i == 2) {
msg[1] = " No. of directories"
msg[2] = sdcnt # no. of sub-dirs
msg[3] = sdsz # their size
}
else if (i == 3) {
# sets separator line to bright black: see also TIP (above)
msg[1] = sprintf("\033[1;30m")
msg[1] = sprintf("%19s","-------")
msg[2] = "--"
msg[3] = "----"
msg[4] = "---"
}
else {
msg[1] = " Total"
msg[2] = (dcnt) # all in directory
msg[3] = (dsz) # size
}
# see also TIP (above) - either
# printf(" \033[47;1;37m")
# or
printf(" "on) # see 'set various' (above)
printf(spc1, msg[1])
if (i != 3) {
printf(sep1,": ")
msg[4] = int(msg[3]/1024) # byte to kbyte
if ((msg[3] % 1024) > 512) msg[4]++ # round-up
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.