This is by way of a tip. I have always used ANSI codes to display colour
error messages or warning signals in batch files etc. When I moved to
Linux I was looking for a way to continue using them. There is plenty of
good stuff on the internet (a search on Google using "\330 ANSI" will
throw up much of it) but not about using it with Awk. 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.
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.
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 (~).
# = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =
# awkdir (Tek-Tips ANSI example for use w. Awk: dmk 12/02)
BEGIN {
while (("ls -AlF" | getline) > 0) {
print crap_sort($0)
dsz = (dsz + $5)
dcnt++
}
close("ls -AlF"
exit
} # Close: BEGIN
END {
# set various
on = "\033[47;1;37m"; off = "\033[0m\n"
sep1 = "%-2s"; sep2 = "%3s"
spc1 = "%-19s"; spc2 = "%4s"
spc3 = "%10s"; spc4 = "%6s"
for (i = 1; i <= 4; i++) rprt_wrtr()
} # Close: END
# FUNCTIONS----------------------------------------------------
function crap_sort(input_str, ftype) {
if (input_str ~ /\/$/ ||
input_str ~ /*$/ ||
input_str ~ /@$/ ||
input_str ~ /\~$/ ||
$10 == "->" {
ftype = substr(input_str,length()) # picks-up last char
input_str = substr($0,1,56) # picks-up 1st eight fields
# sets directory to bright blue
if (ftype == "/" {
input_str = input_str "\033[1;34m" substr($9, 1, (length($9)-1))"\033[0m"
sdcnt++
sdsz = (sdsz + $5)
}
# sets command to bright green
else if (ftype == "*" {
input_str = input_str "\033[1;32m" substr($9, 1, (length($9)-1))"\033[0m"
}
# sets link to yellow
else if (ftype == "@" {
input_str = input_str "\033[33m" substr($9, 1, (length($9)-1))"\033[0m"
}
# sets backup to bright magenta
else if (ftype == "~" {
input_str = input_str "\033[1;35m" substr($9, 1, (length($9)-1))"\033[0m"
}
# sets link in form 'termcap -> /usr/share/misc/termcap' to yellow
else {
input_str = input_str "\033[33m" substr($9, 1, length($9)) "\033[0m "$10" "$11
}
}
return input_str
} # ----------Close: function crap_sort()
function rprt_wrtr( msg) {
# This function writes the report using bright white on gray
# (white) background
# TIP----------------------------------------------------------
# NB When using printf() set attribs separately from the formatting
# eg:
# printf(" \033[47;1;37m" printf("%-20s", "No. of files"
# This example:
# printf("%-20s"," \033[47;1;37mNo. of files"
# does not work well (concatenates the expression, the
# formatting is lost).
# 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
msg[4] = int(msg[3]/1024)
}
else if (i == 2) {
msg[1] = " No. of directories"
msg[2] = sdcnt # no. of sub-dirs
msg[3] = sdsz # their size
msg[4] = int(sdsz/1024)
}
else if (i == 3) {
msg[1] = sprintf("%19s","-------"
msg[2] = sprintf("%4s","--"
msg[3] = sprintf("%10s","----"
msg[4] = sprintf("%6s","---"
}
else {
msg[1] = " Total"
msg[2] = (dcnt) # all in directory
msg[3] = (dsz) # size
msg[4] = int(dsz/1024)
}
if ((msg[3] % 1024) > 512) {msg[4] = msg4++} # round-up
# either
# printf(" \033[47;1;37m"
# or
printf(" "on) # see 'set various' (above)
printf(spc1, msg[1])
if (i != 3) {
printf(sep1, ":"
printf(spc2, numWsep(msg[2]))
printf(sep2, "- "
printf(spc3, numWsep(msg[3]))
printf(" byte: "
printf(spc4, numWsep(msg[4]))
printf(" Kb "
}
else { # line 3: broken line separator
printf(sep1, " "
printf(spc2, msg[2])
printf(sep2," "
printf(spc3, msg[3])
printf(" "
printf(spc4, msg[4])
printf(" "
}
printf(off) # see comment (above)
return 1
} # ----------Close: function rprt_wrtr()
function numWsep(dsz, slngth, dsWsep) {
dsWsep = 0
slngth = length(dsz)
while (slngth > 3) {
dsWsep != 0 ?
dsWsep = substr(dsz, (slngth-2), 3)","dsWsep :
dsWsep = substr(dsz, (slngth-2), 3)
slngth = (slngth - 3)
}
if (slngth > 0) {
dsWsep != 0 ?
dsWsep = substr(dsz, 1, slngth)"," dsWsep :
dsWsep = dsz
}
return dsWsep
} # ----------Close: function numWsep
# = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =
This only covers the colour attributes of ANSI but I hope that someone
finds it useful.
dmk
error messages or warning signals in batch files etc. When I moved to
Linux I was looking for a way to continue using them. There is plenty of
good stuff on the internet (a search on Google using "\330 ANSI" will
throw up much of it) but not about using it with Awk. 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.
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.
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 (~).
# = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =
# awkdir (Tek-Tips ANSI example for use w. Awk: dmk 12/02)
BEGIN {
while (("ls -AlF" | getline) > 0) {
print crap_sort($0)
dsz = (dsz + $5)
dcnt++
}
close("ls -AlF"
exit
} # Close: BEGIN
END {
# set various
on = "\033[47;1;37m"; off = "\033[0m\n"
sep1 = "%-2s"; sep2 = "%3s"
spc1 = "%-19s"; spc2 = "%4s"
spc3 = "%10s"; spc4 = "%6s"
for (i = 1; i <= 4; i++) rprt_wrtr()
} # Close: END
# FUNCTIONS----------------------------------------------------
function crap_sort(input_str, ftype) {
if (input_str ~ /\/$/ ||
input_str ~ /*$/ ||
input_str ~ /@$/ ||
input_str ~ /\~$/ ||
$10 == "->" {
ftype = substr(input_str,length()) # picks-up last char
input_str = substr($0,1,56) # picks-up 1st eight fields
# sets directory to bright blue
if (ftype == "/" {
input_str = input_str "\033[1;34m" substr($9, 1, (length($9)-1))"\033[0m"
sdcnt++
sdsz = (sdsz + $5)
}
# sets command to bright green
else if (ftype == "*" {
input_str = input_str "\033[1;32m" substr($9, 1, (length($9)-1))"\033[0m"
}
# sets link to yellow
else if (ftype == "@" {
input_str = input_str "\033[33m" substr($9, 1, (length($9)-1))"\033[0m"
}
# sets backup to bright magenta
else if (ftype == "~" {
input_str = input_str "\033[1;35m" substr($9, 1, (length($9)-1))"\033[0m"
}
# sets link in form 'termcap -> /usr/share/misc/termcap' to yellow
else {
input_str = input_str "\033[33m" substr($9, 1, length($9)) "\033[0m "$10" "$11
}
}
return input_str
} # ----------Close: function crap_sort()
function rprt_wrtr( msg) {
# This function writes the report using bright white on gray
# (white) background
# TIP----------------------------------------------------------
# NB When using printf() set attribs separately from the formatting
# eg:
# printf(" \033[47;1;37m" printf("%-20s", "No. of files"
# This example:
# printf("%-20s"," \033[47;1;37mNo. of files"
# does not work well (concatenates the expression, the
# formatting is lost).
# 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
msg[4] = int(msg[3]/1024)
}
else if (i == 2) {
msg[1] = " No. of directories"
msg[2] = sdcnt # no. of sub-dirs
msg[3] = sdsz # their size
msg[4] = int(sdsz/1024)
}
else if (i == 3) {
msg[1] = sprintf("%19s","-------"
msg[2] = sprintf("%4s","--"
msg[3] = sprintf("%10s","----"
msg[4] = sprintf("%6s","---"
}
else {
msg[1] = " Total"
msg[2] = (dcnt) # all in directory
msg[3] = (dsz) # size
msg[4] = int(dsz/1024)
}
if ((msg[3] % 1024) > 512) {msg[4] = msg4++} # round-up
# either
# printf(" \033[47;1;37m"
# or
printf(" "on) # see 'set various' (above)
printf(spc1, msg[1])
if (i != 3) {
printf(sep1, ":"
printf(spc2, numWsep(msg[2]))
printf(sep2, "- "
printf(spc3, numWsep(msg[3]))
printf(" byte: "
printf(spc4, numWsep(msg[4]))
printf(" Kb "
}
else { # line 3: broken line separator
printf(sep1, " "
printf(spc2, msg[2])
printf(sep2," "
printf(spc3, msg[3])
printf(" "
printf(spc4, msg[4])
printf(" "
}
printf(off) # see comment (above)
return 1
} # ----------Close: function rprt_wrtr()
function numWsep(dsz, slngth, dsWsep) {
dsWsep = 0
slngth = length(dsz)
while (slngth > 3) {
dsWsep != 0 ?
dsWsep = substr(dsz, (slngth-2), 3)","dsWsep :
dsWsep = substr(dsz, (slngth-2), 3)
slngth = (slngth - 3)
}
if (slngth > 0) {
dsWsep != 0 ?
dsWsep = substr(dsz, 1, slngth)"," dsWsep :
dsWsep = dsz
}
return dsWsep
} # ----------Close: function numWsep
# = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - =
This only covers the colour attributes of ANSI but I hope that someone
finds it useful.
dmk