Operating System - HP-UX
1833875 Members
1782 Online
110063 Solutions
New Discussion

grep .profile * ? yields all lines with profile as well ? Only need files with (dot) .profile ?

 
SOLVED
Go to solution
Sammy_2
Super Advisor

grep .profile * ? yields all lines with profile as well ? Only need files with (dot) .profile ?

trying to grep the word .profile from all files in current directory. But
# grep .profile *
shows all lines with profile in it.
How do just grep for lines in file with the dot profile (.profile)

Thanks
good judgement comes from experience and experience comes from bad judgement.
6 REPLIES 6
Bill Hassell
Honored Contributor

Re: grep .profile * ? yields all lines with profile as well ? Only need files with (dot) .profile ?

There are several characters that have special meanings (like * and ? and .). To search for a literal . you must escape the special meaning from the shell:

# grep \.profile *
# grep ".profile" *

man regexp will cover the special characters used for pattern matching.


Bill Hassell, sysadmin
Sammy_2
Super Advisor

Re: grep .profile * ? yields all lines with profile as well ? Only need files with (dot) .profile ?

Bill,
actually, That was what i had thought. But I had earlier tried both the commands as specified by you but I still get lines with profile in it. Don't have unix box in front of me so I could give you an example. Does that work on your unix server ?
Thanks.
good judgement comes from experience and experience comes from bad judgement.
H.Merijn Brand (procura
Honored Contributor
Solution

Re: grep .profile * ? yields all lines with profile as well ? Only need files with (dot) .profile ?

If you don't want special characters at all, use fgrep

# fgrep .profile *

equals

# grep -F .profile *

equals (almost the same, but the effect is the same)

# grep '\.profile' *

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Bill Hassell
Honored Contributor

Re: grep .profile * ? yields all lines with profile as well ? Only need files with (dot) .profile ?

Yes, I checked it out on 2 files that had profile and .profile respectively. Start by checking out your grep with the type command:

$ type grep
grep is /usr/bin/grep

This verifies what you will get when you type grep on the command line (essentially obsoletes which and whereis). You might have an alternate grep installed. The suggestion to use fgrep (or grep -F) is a good one too.


Bill Hassell, sysadmin
Sammy_2
Super Advisor

Re: grep .profile * ? yields all lines with profile as well ? Only need files with (dot) .profile ?

Procura- Bingo !! Hit the nail on the head with fgrep or grep -F. full points if it lets me assign it.
Bill- Tried the 2 commands but yield similar results (see below)
But grep -F would work

# which grep
/usr/bin/grep
# grep \.subir *
vic.pl:#system ("echo DATE is `date` >> $vic_log 2>&1;cat $vic_log | mailx -s '$
vp' subir_pokhriyal\@dom.com 2>&1");
vic1.pl:$int_org="/home/paradigm/scripts/VIC/vic1.load" .subir test ;
good judgement comes from experience and experience comes from bad judgement.
Sammy_2
Super Advisor

Re: grep .profile * ? yields all lines with profile as well ? Only need files with (dot) .profile ?

Thanks to Procure and Bill Hassell for quick and information responses. Resolution provided in earlier response
good judgement comes from experience and experience comes from bad judgement.