Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

easiest way to solve search problem

Status
Not open for further replies.

djr111

Technical User
Aug 5, 2006
357
US

Ok, this is probably a bonehead question, but here it goes....

I want to pull file name from a directory. Unique names not all that have that name in it.



grep setp04 /tmp/filename


EG:

sept04
sept04-misc
setp04-reports

grep will find all, what are my options to capture spet04 only. I did not find any switches that would help. So I'm guessing another way besides grep must be used?



Thanks


 
I don't get it... If you already have the filename, why do you need to grep?


HTH,

p5wizard
 
I did try -x using /usr/xp44/bin grep versus the default grep which is /usr/bin/grep, but it seem to eliminate. can you please try to put some logic on what is happening, because I can not figure why it is doing this.

two examples give two different results.

example one (more like I need to do it)

/usr/xpg4/bin/grep servername
/var/log/syslog
Mar 10 14:08:53 servername-1.yourcompany.com logmgr: ID = 3032 : Wed Mar 10 14:06:17 2010 : .......

Mar 10 14:08:53 servername-2.yourcompany.com logmgr: ID = 3032 : Wed Mar 10 14:06:17 2010


"-with -x flag"

/usr/xpg4/bin/grep -x servername /var/log/syslog

nothing is outputted, unlke below it works.

note: I just modified the identities of the hosts, everything else is cut and paste..

=========
/usr/xpg4/bin/grep fileA tfile
fileA
fileA-old
fileA-test


"with -x flag"
/usr/xpg4/bin/grep -x fileA tfile
fileA



 
I know in my example I know the file name. This will just be a piece within a script that will query. The file will not nessarily be there.
 
The file will not necessarily be there...

So, test for existence of the file?

[tt]if test -f ${DIR}/${FILE}
then
echo ${FILE} found in ${DIR}
else
echo ${FILE} not in ${DIR}
fi[/tt]

HTH,

p5wizard
 
Read the grep man page and you'll understand why -x behaves like that.



Annihilannic.
 
Hi

Now it sounds like a task for the -w option :
man grep said:
-w, --word-regexp
Select only those lines containing matches that form whole
words. The test is that the matching substring must either be
at the beginning of the line, or preceded by a non-word
constituent character. Similarly, it must be either at the end
of the line or followed by a non-word constituent character.
Word-constituent characters are letters, digits, and the
underscore.
But note that I understand nothing from your goal.

Feherke.
 
Hi I just restructured the script to use other logic. It is a script that pre dates me and I am only trying to clean it up a little.

Thanks for the info all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top