hilite.rexx
I often need to look for something in the output of a program. In
Linux, I sometimes use grep, but there are times that I need to see
the entire output, and not just the line containing the string.
So, I decided to write the following tool, called 'hilite'. In
operation, you can pipe the output of a program to it, and it will
hilite whatever search string you are looking for.
The current method of highlighting is to output a 'marker string'
immediately underneath the line containing the search string. It will
highlight one or more occurrences of the string, and has options to
ignore case and to change the marker character.
I would rather highlight by changing the font directly in the line
containing the search string, say by making the match print in bold,
underline, or a different colour, but I don't know how to do that in
the terminal (Fedora, Gnome Terminal).
Anyway, here it is. Please feel free to offer criticisms or
==================================
#! /usr/bin/rexx
ignorecase = 0
hilitechar = "^"
parse arg cmdline
if cmdline = "" then call usage
do i = 1 to words(cmdline)
cmdword = word(cmdline,i)
if left(cmdword,1) = '-' then
do j=2 to length(cmdword)
select
when substr(cmdword,j,1) = "i" then
ignorecase = 1
when substr(cmdword,j,1) = "h" then do
hilitechar = substr(cmdword,j+1,1)
say j
j = j+1
end
end
end
else
srchstr = cmdword
end
do until lines() = 0
x = linein()
say x
if ignorecase then do
x = translate(x)
srchstr = translate(srchstr)
end idx = index(x,srchstr)
if idx > 0 then do
say markit(idx, x, srchstr)
end
end
exit
markit:
ix = arg(1)
s = arg(2)
srch = arg(3) strt = 1
do while ix > 0
s=overlay("",s,strt,ix-strt," ")
s=overlay("",s,ix,length(srch),left(hilitechar,1))
strt = ix + length(srch)
ix = index(s,srch)
end
return overlay("",s,strt,length(s) - strt + 1," ")
usage: procedure say
say 'Usage: hilite [options] <search string>'
say ' options: -i ignore case'
say ' -h change hilite character'
say 'Examples: hilite -ih"|" foo'
say ' hilite -i -h"|" foo'
say ' hilite -hX CamelCase'
say
exit
|