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

regexp string delimiter

Status
Not open for further replies.

pcorchary

MIS
Feb 23, 2002
60
US
Help!

I need a grep only solution to this ... how to search for a string literal?

Suppose the input file of:

test blah (tabs or spaces or both seperate)
test-foo blah
foo-test blah
test-bar boop

And I want to grep for "test", and have ONLY the first line returned ....

Thanks in advance!
 
How about:

grep "^test " your_file

-jim
 
Not too sure of your question, but I'll try...

if you want to grep for the word "test" (not the string)then

grep -w "test" inputfile

Now, if the word "test" is present in the inputfile and you want to print the first line only, then...

test `grep -w "test" inputfile` && head -1 inputfile

untested.
 
Nope ... sorry. Those don't work. Already been down this path. Perhaps I should have said that I've been working with grep for 15 years, andI spent many hours trying to get this working before posting :)

Attempts like above match every line in the input_file that contain "test". I want to match ONLY lines that contain "test" by itself, not "test-foo" or "footest-bar", etc. And I can't count on the char that follows the string, nor the char that precededs it.

Hope someone can help! Thanks!
 
What does "by itself" mean? If it means test must be separated from anything else on the line by spaces and/or tabs, try

grep '^test[ ]+|[ ]+test[ ]+|[ ]+test$' filename

where the square brackets contain a space and a tab. CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
hmmm ... doesn't seem to work. One question - how to refer to a tab in grep? (obviously in regexp or e-regexp).

Here's the sample file
footest-bar blah
test-bar blah
test blah***
test-test google
testblah yaya
test crap***
foo-test blah

I want to in this case match only the *** lines
 
you can either use 'egrep' OR 'grep -e' and specify a 'character class' like so:

egrep '^test[:space:]+|[:space:]+test[:space:]+|[:space:]+test$' filename

'man -s 5 regex' vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 

Yeah, I should have said egrep instead of grep. To specify a tab, just hit the tab key i.e. in the [ ] type space then tab. Vlad's method does the same but a little more elegantly. CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top