Plan 9 from Bell Labs’s /usr/web/sources/contrib/fgb/root/sys/src/cmd/4th/examples/fgrep.4th

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


\ 4tH fgrep utility - Copyright 2003, 2004 J.L. Bezemer
\ You can redistribute this file and/or modify it under
\ the terms of the GNU General Public License

[needs lib/search.4th]
[needs lib/argopen.4th]
                                       \ process the input file line by line
: ProcessFile                          ( n --) 
  begin
    1+ refill                          \ increase linenumber and read the line
  while                                \ save linenumber
    >r 0 parse 2dup 1 args search      \ parse the line and get the pattern
    nip nip                            \ if found display
    if r@ 5 .r [char] : emit space type cr
    else 2drop                         \ if not, drop
    then r>                            \ return the linenumber
  repeat drop                          \ drop the linenumber
;

: fGrep                                \ open input and output files
  argn 3 < abort" fgrep pattern file"  \ issue message if error
  argn 2 do
    input i arg-open                   \ open the input file
    ." >>> " i args type cr            \ show the filename
    0 ProcessFile                      \ process the file
    close                              \ close the file
  loop
;

fGrep

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.