Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > hilite.rexx
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 2nd April 19:02
oleg lego
External User
 
Posts: 1
Default 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
  Reply With Quote


  sponsored links


2 2nd April 23:09
allodoxaphobia
External User
 
Posts: 1
Default hilite.rexx



grep -A 10 -B 10 foo bar

Jonesy
--
Marvin L Jones | jonz | W3DHJ | linux
38.24N 104.55W | @ config.com | Jonesy | OS/2
*** Killfiling google posts: <http://jonz.net/ng.htm>
  Reply With Quote
3 2nd April 23:09
oleg lego
External User
 
Posts: 1
Default hilite.rexx


On 16 Feb 2007 21:34:16 GMT, Allodoxaphobia posted:


Thanks for the comment. I tried that already, but it has several
drawbacks.

It still gives me a 'wall of text' that I have to search through to
find the matches, and once I find the line, I have to read through the
whole thing carefully in order to find multiple occurrences. It also
doesn't give me the entire output

Originally, I wrote it to look at some shared libraries (using nm),
that had some incredibly long function names, producing lines as long
as 100 characters.
  Reply With Quote
4 3rd April 03:19
imc@comlab.ox.ac.uk (ian
External User
 
Posts: 1
Default hilite.rexx


Pipe the output to "less -j10" and then use "/foo" to search for foo...

esc="1b"x; hi=esc"[1m"; lo=esc"[m";
say "this is a" hi"highlighted"lo "word"

--
---- Ian Collier : imc@comlab.ox.ac.uk : WWW page (including REXX section):
------ http://users.comlab.ox.ac.uk/ian.collier/imc.shtml

New to this group? Answers to frequently-asked questions can be had from
http://www.rexxla.org/faq.html .
  Reply With Quote
5 3rd April 03:19
oleg lego
External User
 
Posts: 1
Default hilite.rexx


On Sat, 17 Feb 2007 23:49:45 +0000 (UTC), Ian Collier posted:


Nice. Never noticed that about "less" before (i'm just getting back
into Linux again, after many years away).


Ahh! Never thought to try VT ESC sequences. Thanks. I also noticed a
problem with my code when a line has one or more tabs in it. The
highlighting within the line itself will mean I don't have to deal
with them at all.

Thanks!
  Reply With Quote
6 3rd April 03:19
john small
External User
 
Posts: 1
Default hilite.rexx


FWIW, here's a modified version of your code that uses reverse video
to highlight:

ignorecase = 0

parse arg cmdline
if cmdline = "" then call usage

do i = 1 to words(cmdline)
cmdword = word(cmdline,i)
if left(cmdword,1) = '-' then
if cmdword == '-i' then
ignorecase = 1
else
call usage
else
srchstr = cmdword
end

esc = d2c(27)
normalclr = esc || '[0m'
reverseclr = esc || '[7m'
srchlen = length(srchstr)
uppersrch = translate(srchstr)
do until lines() = 0
x = linein()
if ignorecase then do
upperx = translate(x)
idx = pos(uppersrch, upperx)
end
else
idx = pos(srchstr, x) strt = 1
if idx > 0 then do
do until idx == 0
call charout , substr(x, strt, idx - strt) || reverseclr ||
substr(x, idx, srchlen) || normalclr
strt = idx + srchlen
if ignorecase then
idx = pos(uppersrch, upperx, strt)
else
idx = pos(srchstr, x, strt)
end
say substr(x, strt)
end
else
say x
end

exit
usage: procedure say
say 'Usage: hilite [options] <search string>'
say ' options: -i ignore case'
say
say 'Examples: hilite -i foo'
say ' hilite foo'
say
exit

--

John Small

(remove the z's for email address)
  Reply With Quote
7 3rd April 07:54
oleg lego
External User
 
Posts: 1
Default hilite.rexx


On Sun, 18 Feb 2007 10:58:16 -0000, John Small posted:

Thanks John. That looks great.
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666