Re: egrep question
- From: "Hunter, Mark" <Mark.Hunter@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 7 Feb 2007 10:23:14 -0600
The trick to using grep and egrep effectively is to truly understand how pattern
matching works.
Some good examples online are at http://www.robelle.com/smugbook/regexpr.html
In general, I find the O'Reilly books pretty good.
Mark Hunter
Anheuser-Busch Cos.
MIS Consultant, ES&SO Server Planning and Integration
*Office: (314) 765-2339
*Email: Mark.Hunter@xxxxxxxxxxxxxxxxxx
The information transmitted (including attachments) is covered by the Electronic
Communications Privacy Act, 18 U.S.C. 2510-2521, is intended only for the
person(s) or entity/entities to which it is addressed and may contain
confidential and/or privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon, this
information by persons or entities other than the intended recipient(s) is
prohibited. If you received this in error, please contact the sender and delete
the material from any computer.
-----Original Message-----
From: IBM AIX Discussion List [mailto:aix-l@xxxxxxxxxxxxx] On Behalf Of
Andrew.Townsend@xxxxxxxxx
Sent: Wednesday, February 07, 2007 9:43 AM
To: aix-l@xxxxxxxxxxxxx
Subject: Re: egrep question
Mark, I use awk for one-liners but haven't found a great resource on how to
exploit grep. Do you have any recommendations?
I personally would not use grep but awk instead, especially if file1 and
file2 are reasonably large (awk is fast). If you use egrep, make sure you egrep
for '^$name$' since you don't want a book called Tom in file1 to match a book
called Tom Sawyer in file2.
Sample awk code to do your search
#!/bin/ksh
# do your error checking here
file1=$1
file2=$2
awk -v DATAFILE1=$file1 -v DATAFILE2=$file2 '
BEGIN{
while (getline < DATAFILE2 > 0){
bookname[$0]=1;}
}
{
if (bookname[$0] == 1)
print $0 " is IN " DATAFILE2
else
print $0 " NOT in " DATAFILE2 }' $file1 If file1 contains:
Tom Sawyer
Tale of Two Cities
Sorc. and the . Man
Etc
Ampad
and file2 contains
Tale of Two Cities
Sorc. and the . Man
Tom Sawyer
Mazing
Ampad Inc
the output is:
Tom Sawyer is in f2
Tale of Two Cities is in f2
Sorc. and the . Man is in f2
Etc is not in f2
Ampad is not in f2
Note that there are entries in file2 that are not in file1. I assume you are
not checking the inverse case intentionally.
Mark Hunter
Anheuser-Busch Cos.
From: IBM AIX Discussion List [mailto:aix-l@xxxxxxxxxxxxx] On Behalf Of Denny
Watkins
Sent: Wednesday, February 07, 2007 8:13 AM
To: aix-l@xxxxxxxxxxxxx
Subject: egrep question
I have two files which have titles of books.
I want to check the book titles in one file to see if the title is in the other
file.
The book titles have spaces, periods, etc.
I wrote a script but I am having a problem getting the title in a form to
'egrep' to the other file.
Titles examples are like:
Tom Sawyer
Tale Of Two Cities
Script is called "check.file.to.file" and is as follows
and is run check.file.to.file name-of-file-1 name-of-file-2
1
2 if [ "$1" = "" ]
3 then
4 arg=0
5 echo "No args..Args are file-name file-to-check"
6 exit
7 else
8 arg=1
9 file1=$1
10 fi
11
12 if [ "$2" = "" ]
13 then
14 echo "Need 2nd argument for file-to-check"
15 exit
16 else
17 file2=$2
18 fi
19
20 # for name in `cat $file1 | cut -f1 -d' '`
21
22 for name in `cat $file1`
23 do
24
25 # egrep -w $name $2 > /dev/null
26 egrep $name $2 > /dev/null
27 grepresult=$?
28
29
30 if (($grepresult == 0))
31 then
32 echo $name " is IN "$2
33 # egrep -w $name $2
34 else
35 echo $name " NOT in "$2
36 fi
37
38 done
Is seems the problem is at line 32. How do you make the $name variable in line
32 quoted?
Thanks,
Denny Watkins
Morningside College
Phone: 712-274-5250
Email: watkins@xxxxxxxxxxxxxxx
The information transmitted (including attachments) is covered by the Electronic
Communications Privacy Act,
18 U.S.C. 2510-2521, is intended only for the person(s) or entity/entities to
which it is addressed and may contain confidential and/or privileged material.
Any review, retransmission, dissemination or other use of, or taking of any
action in reliance upon, this information by persons or entities other than the
intended recipient(s) is prohibited.
If you received this in error, please contact the sender and delete the material
from any computer.
The information transmitted (including attachments) is
covered by the Electronic Communications Privacy Act,
18 U.S.C. 2510-2521, is intended only for the person(s) or
entity/entities to which it is addressed and may contain
confidential and/or privileged material. Any review,
retransmission, dissemination or other use of, or taking
of any action in reliance upon, this information by persons
or entities other than the intended recipient(s) is prohibited.
If you received this in error, please contact the sender and
delete the material from any computer.
- References:
- Re: egrep question
- From: Hunter, Mark
- Re: egrep question
- From: Andrew . Townsend
- Re: egrep question
- Prev by Date: Re: egrep question
- Next by Date: Are there other web sites with the AIX-L list on-line?
- Previous by thread: Re: egrep question
- Next by thread: Re: egrep question
- Index(es):
Relevant Pages
|
|