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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grep 1

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
GB
why can't I run the command grep -i (string)

I seem to get errors with the (

Also what is the symbol for or | in unix?

Via a telnet session

Thanks in advance

Chris
 
put quotes '(..)'
or mask \(..\) -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Thanks for that it half worked.

If I put grep -i (proc) chris.txt

I get an error


If I put grep -i proc chris.txt

I get some lines returned


If I put grep -i '(proc)' chris.txt

I get no lines returned.



 
your textfile does not contain this: (string) -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
The reason for the error is that parentheses have a special meaning in Unix shells, just as quotes do. If you don't quote or escape (\) them, the shell tries to interpret what's between them in a particular way. When that interpretation fails, you get the error from the shell, before grep is even called.
 
Jamisar

Test two said it did. Or are we talking about something different?
 
No one answered you on the | (pipe) symbol used in sun.
| a connector of 2 or more commands

for example:
1. who | finger | sort - this displays who but puts it in finger for you to see where the user is logged in from and sort sorts it by user login name
2. ls |more - displays the contents of the directory but pauses it a page at a time (much like dir /p in dos)
3. ps -e |grep user1 - displays processes of user1 only

Hope this helps.
 
The proper command is:

grep -i pattern file

grep -i does a case insensitive grep.
pattern is the pattern you are searching for.
file is the file you are searching in.

Unless you have spaces or special characters, you do not need '(" or anything of the like.
If you do have something special in there, like ( or #, then encase the entire pattern in '. Like this

grep -i '#wtrepani' /etc/passwd

Another way of explaining the |, is that it pipes the output from one command thru the next in line, ie

cat /etc/passwd |grep -v '#' |grep '/home/yoda' |awk {'print $3'}

will cat the file, then remove any lines with #, then of the remaining lines only select those that contain /home/yoda, then output the 3rd field of that. It will process the first command first then use the output of that to process the second.

Thanks

Will
 
Sorry, I worded my question about the | symbol really badly. I didn't intend it to be for the "pipe" function. I was talking about | meaning OR in the regular expression sense.

That is gr(e|a)y to mean grey or gray. I was wondering if you could use such regular expression constructs using grep or egrep. I couldn't find the correct symbol.

Thanks for all the replies.
 
egrep "gr(e|a)y" should work. (Or grep -E on some systems). The 'e' version of grep uses extended regular expressions. If it's just one character that could vary in the expression you could use normal grep gr[ea]y. Annihilannic.
 
Thanks for the double quotes it seems that was what I was looking for.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top