[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: awk or sed?



On Tue, Apr 07, 1998 at 12:55:48AM -0700, George Bonser wrote:
> On Tue, 7 Apr 1998, George Bonser wrote:
> 
> > Hehe, try grep!
> > 
> > grep -vFf file2 file1
> > 
> > should do it.
> 
> For newbies:
> 
> The original poster wants to filter file1 using file2. The output should
> contain everything in the input that is NOT matched by the pattern. That
> means we are going to need a v option to grep.  Now, the pattern is a list
> of items separated by newlines so we are going to need to tell grep this
> with the F option. Finally, the pattern is contained in a file. We tell
> grep the name of that file with the f option followed immediately with the
> filename so:
> 
> grep -vFf <pattern-file> <input file>
> 
> means:
> 
> tell me everything that DOES NOT match what is in the newline delimited
> file <pattern-file> contained in <input-file>
> 
> 
> 
> George Bonser 
> Just be thankful that Open Group does not own Linux.
> http://www.debian.org
> Debian/GNU Linux ... the maintainable operating system.
> 

This will also delete every line that just match partially such as alpha2 or
hello-alpha. The correction will be to edited the pattern-file a little through
sed before submit it to grep:

sed -e 's/^/^/' -e 's/$/$/' <pattern-file> | grep -vf - <input file>

^ and $ stand for 'match begin/end of line' respectively. The sed part
(sed -e 's/^/^/' -e 's/$/$' <pattern-file>) say "replace the beginning
of each line with '^' and replace the end of line with '$'". They are
then send to grep throughout the -f - (- are often used to mean take
file from standard input) and then apply to each line of <input file>.
Note that I removed the -F to let the default extend expression applied.
(The extended grep understand '^' and '$' meaning, but the fast(-F)
grep dont).

Note also that the reverse (finding all lines in file1 that match those
in file2) are easier to acheive through the join utility.
Litteraly is 'join file1 file2'.

-- 
------------------------------------------------------------------------
Fabien Ninoles                                  Running Debian/GNU Linux
E-mail:                                                    fab@tzone.org
WebPage:                      http://www.callisto.si.usherb.ca/~94246757
WorkStation [available when connected!]:     http://nightbird.tzone.org/
RSA PGP KEY [E3723845]: 1C C1 4F A6 EE E5 4D 99  4F 80 2D 2D 1F 85 C1 70
------------------------------------------------------------------------

Attachment: pgpCkSFGbQZQ5.pgp
Description: PGP signature


Reply to: