🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: net.micro.att
Article #1050

fgrep on the UNIX-PC

#1050
From: William H. Huggins
Date: Mon, 23 Jun 1986 21:59
90 lines
3158 bytes
One of the serious blunders that someone made in releasing the
UNIX-PC software was the elimination of egrep and fgrep.
(They simply made links to grep -- which is a swindle because
egrep and fgrep do different things than grep in a REAL UNIX
system).  The program  fgrep  allows the user to specify not
just one expression but multiple expressions one to a line,
and it then finds lines that contain any of these expressions, e.g.

	$ fgrep '
		expr1
		expr2
		expr3
		...
		' files...

This is more useful than grep, since one may not be able to state
precisely ahead of time what strings to look for, but may desire
any of several alternatives.

Now, this can also be done using:

	$ sed -n '
	/expr1/p
	/expr2/p
	/expr3/p
	' file1 file2 file3 ...

which prints any line that contains any of these three expressions.
The trouble with this command is that, although it will print out the
lines containing any of the three expressions, it will not
print the name of the particular file in which they are located.

So, let's use sed on just one file at a time. This can be done by the
little shell program:

	for i in file1 file2 file3 ...
	do	echo "In $i:"
		sed -n '
		/expr1/p
		/expr2/p
		/expr3/p
		' $i
	done

which examines each of the files in order; prints out "In file1:"
followed by any lines in  file1 that match the given exprs; then 
examines file2, etc. 

This begins to resemble the missing program fgrep.  So let's write
a little shell program which we shall call  'fgrep' .  It should 
look something like:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# fgrep --  emulates the 'real' fgrep  for a  single input file.

case $# in
0)	cat <<-endmsg
	Usage:  fgrep expr1 [expr2 ...] file
	
	        Prints lines of  file  that contain any of
	        the expressions (ed-style syntax).
	endmsg
	exit ;;
*)	;;
esac

# prepare sed program for finding  expr1 expr2 etc.
echo "sed -n '" >sed$$
while test "k$2" != "k"		# There is yet another expression
do	echo "/$1/p">>sed$$
	shift
done
echo "' $1" >>sed$$		# append file name to sed$$ program

# execute sed$$ to search for exprs in file.
. sed$$

rm sed$$			# remove temporary file sed$$

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

The above program writes the sed program needed to search
for the desired exprs  and then it runs the very program it has
written.

This shows how a little shell file using the Tools that are
available can be written to do more useful things -- in this
case to emulate the missing  'fgrep'  command.
-- 
W.H. Huggins 
Technical Details
Message-ID: <3052@jhunix.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!seismo!umcp-cs!aplcen!jhunix!whh

Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!seismo!umcp-cs!aplcen!jhunix!whh
From: whh@jhunix.UUCP (William H. Huggins)
Newsgroups: net.micro.att
Subject: fgrep on the UNIX-PC
Message-ID: <3052@jhunix.UUCP>
Date: Mon, 23-Jun-86 17:59:39 EDT
Article-I.D.: jhunix.3052
Posted: Mon Jun 23 17:59:39 1986
Date-Received: Thu, 26-Jun-86 02:45:05 EDT
Distribution: net
Organization: Johns Hopkins Univ. Computing Ctr.
Lines: 90