Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Towards more colourful Awk scripting: using ANSI 2

Status
Not open for further replies.

menski

Technical User
Oct 13, 2002
18
GB
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 == &quot;->&quot;) {

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 == &quot;/&quot;) {
input_str = input_str &quot;\033[1;34m&quot; substr($9, 1, (length($9)-1))&quot;\033[0m&quot;
sdcnt++
sdsz = (sdsz + $5)
}

# sets command to bright green
else if (ftype == &quot;*&quot;) {
input_str = input_str &quot;\033[1;32m&quot; substr($9, 1, (length($9)-1))&quot;\033[0m&quot;
}

# sets link to yellow
else if (ftype == &quot;@&quot;) {
input_str = input_str &quot;\033[33m&quot; substr($9, 1, (length($9)-1))&quot;\033[0m&quot;
}

# sets backup to bright magenta
else if (ftype == &quot;~&quot;) {
input_str = input_str &quot;\033[1;35m&quot; substr($9, 1, (length($9)-1))&quot;\033[0m&quot;
}

# sets link in form 'termcap -> /usr/share/misc/termcap' to yellow
else {
input_str = input_str &quot;\033[33m&quot; substr($9, 1, length($9)) &quot;\033[0m &quot;$10&quot; &quot;$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(&quot; \033[47;1;37m&quot;); printf(&quot;%-20s&quot;, &quot;No. of files&quot;)

# This example:

# printf(&quot;%-20s&quot;,&quot; \033[47;1;37mNo. of files&quot;

# does not work well (concatenates the expression, the
# formatting is lost).

# END of TIP---------------------------------------------------

if (i == 1) {
msg[1] = &quot; No. of files&quot;
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] = &quot; No. of directories&quot;
msg[2] = sdcnt # no. of sub-dirs
msg[3] = sdsz # their size
msg[4] = int(sdsz/1024)
}
else if (i == 3) {
msg[1] = sprintf(&quot;%19s&quot;,&quot;-------&quot;)
msg[2] = sprintf(&quot;%4s&quot;,&quot;--&quot;)
msg[3] = sprintf(&quot;%10s&quot;,&quot;----&quot;)
msg[4] = sprintf(&quot;%6s&quot;,&quot;---&quot;)
}
else {
msg[1] = &quot; Total&quot;
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(&quot; \033[47;1;37m&quot;)
# or
printf(&quot; &quot;on) # see 'set various' (above)

printf(spc1, msg[1])
if (i != 3) {
printf(sep1, &quot;:&quot;)
printf(spc2, numWsep(msg[2]))
printf(sep2, &quot;- &quot;)
printf(spc3, numWsep(msg[3]))
printf(&quot; byte: &quot;)
printf(spc4, numWsep(msg[4]))
printf(&quot; Kb &quot;)
}
else { # line 3: broken line separator
printf(sep1, &quot; &quot;)
printf(spc2, msg[2])
printf(sep2,&quot; &quot;)
printf(spc3, msg[3])
printf(&quot; &quot;)
printf(spc4, msg[4])
printf(&quot; &quot;)
}
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)&quot;,&quot;dsWsep :
dsWsep = substr(dsz, (slngth-2), 3)
slngth = (slngth - 3)
}

if (slngth > 0) {
dsWsep != 0 ?
dsWsep = substr(dsz, 1, slngth)&quot;,&quot; 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



 
Very good - Put it in the FAQ ! Dickie Bird (:)-)))
 

Um - thank you! I'm glad you like it - but I don't have a clue as to how to turn it into an FAQ (and HAS anyone ever asked the question?)

dmk
 
click on the &quot;FAQs&quot; tab in this forum OR
clink on the &quot;Write your own AWK FAQ&quot; in the &quot;Reply&quot; pane in this thread. vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I did some of this a year ago - I ended up searching Usenet. It's nice to have a readable explanation and a working example. This would be a great FAQ, thanks. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 

OK Chaps - it's done (now you must all (both of you) vote it brilliant). Glad you found it useful!

dmk
 
I'm not sure if I'm using this script properly, however I copied the script into a file and executed the following from the command line:

awk -f file

Awk returned the following errors:

awk: syntax error near line 3
awk: illegal statement near line 3
awk: syntax error near line 9
awk: bailing out near line 9

Is there something that I'm doing wrong?

Thanks,

John
 
[as always]: if on Solaris, try nawk [instead of awk]. vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
I have tried it on Solaris and AIX using both awk and nawk and I keep getting errors.

awk: 0602-521 There is a regular expression error.
?*+ not preceded by valid expression.

The source line number is 30.
The error context is
input_str ~ >>> /*$/ <<< ||

Thanks,

John
 
Man, that Sun constipation hurts a lot of people.
Maybe you should just download the stable gawk???
 
Is there any way to use this and output it to a file? I attempted to add the following line to a script, however it does not work properly.

echo '\033[1;41m *** STOP *** \033[0m' >> test.out

The output looks like this:

^[[1;41m *** STOP *** ^[[0m

Are these echo commands only useful for console messages? Any help would be greatly appreciated.

Thanks,

John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top