Newbie: Finding An Exact Substring Match In A File ???

Newbie: Finding An Exact Substring Match In A File ???

Post by Ken Abrahamse » Thu, 16 Mar 2000 04:00:00



I have a file which contains strings which are very close to each other (ie;
many strings are actually substrings of others). Other than looping through
each line in the file to check for an *exact* match, is there a better way
of testing to see if there's an *exact* match for a string in ths file.
For example:

File is:
xxxxx1111
xxxxx1111111
xxaaaaaaa
xxxxxthree

For example, if I test this file for the substring 'xx', the test *should*
fail as there is no record with this value. Using 'grep' for 'xx' would find
'xx' as it occurs in this file when what I need is for this test to fail as
none of the records is an exact match to 'xx'.

I can do a loop but was hoping there'd be a better way.

Any suggestions would be appreciated.
TIA.
ken

 
 
 

Newbie: Finding An Exact Substring Match In A File ???

Post by Matthew Land » Thu, 16 Mar 2000 04:00:00



> I have a file which contains strings which are very close to each other (ie;
> many strings are actually substrings of others). Other than looping through
> each line in the file to check for an *exact* match, is there a better way
> of testing to see if there's an *exact* match for a string in ths file.
> For example:

> File is:
> xxxxx1111
> xxxxx1111111
> xxaaaaaaa
> xxxxxthree

> For example, if I test this file for the substring 'xx', the test *should*
> fail as there is no record with this value. Using 'grep' for 'xx' would find
> 'xx' as it occurs in this file when what I need is for this test to fail as
> none of the records is an exact match to 'xx'.

> I can do a loop but was hoping there'd be a better way.

> Any suggestions would be appreciated.
> TIA.
> ken

If the ENTIRE line is to match exactly:
grep -x "xx"

If it is a WORD on a line you want to try to match
grep -w "xx"

The word method has its drawbacks though.  Just about any non alpha numeric

with "xx-aa" would get matched where "xxaa" would not.

 - Matt

--

  AIX and HACMP Certified Specialist    | |  / \ |\| |  \. ,_|    _| --
  / Comments, views, and opinions \     | |_/ ^ \|   | ) | |      \, *_)
  \ are mine alone, not IBM's.    /     |___|/~\_\_|\|__/|_|        \(

 
 
 

Newbie: Finding An Exact Substring Match In A File ???

Post by John Gord » Thu, 16 Mar 2000 04:00:00



> File is:
> xxxxx1111
> xxxxx1111111
> xxaaaaaaa
> xxxxxthree
> For example, if I test this file for the substring 'xx', the test *should*
> fail as there is no record with this value. Using 'grep' for 'xx' would find
> 'xx' as it occurs in this file when what I need is for this test to fail as
> none of the records is an exact match to 'xx'.

^ is a special character meaning "beginning of line"
$ is a special character meaning "end of line"

grep '^xx$' file

---
John Gordon                  "No Silicon Heaven?  Preposterous!  Where would

 
 
 

Newbie: Finding An Exact Substring Match In A File ???

Post by bmar.. » Thu, 16 Mar 2000 04:00:00



   >I have a file which contains
   >strings which are very close to each other (ie; many strings are
   >actually substrings of others). Other than looping through each
   >line in the file to check for an *exact* match, is there a better
   >way of testing to see if there's an *exact* match for a string in
   >ths file. For example:
   >File is:
   >xxxxx1111
   >xxxxx1111111
   >xxaaaaaaa
   >xxxxxthree
   >For example, if I test this file for the substring 'xx', the test
   >*should* fail as there is no record with this value. Using 'grep'
   >for 'xx' would find 'xx' as it occurs in this file when what I need
   >is for this test to fail as none of the records is an exact match
   >to 'xx'.

grep '^xx$'
fgrep -x xx

Net-Tamer V 1.08X - Test Drive

 
 
 

Newbie: Finding An Exact Substring Match In A File ???

Post by Bernard Chandle » Fri, 17 Mar 2000 04:00:00



> I have a file which contains strings which are very close to each other (ie;
> many strings are actually substrings of others). Other than looping through
> each line in the file to check for an *exact* match, is there a better way
> of testing to see if there's an *exact* match for a string in ths file.
> For example:

> File is:
> xxxxx1111
> xxxxx1111111
> xxaaaaaaa
> xxxxxthree

> For example, if I test this file for the substring 'xx', the test *should*
> fail as there is no record with this value. Using 'grep' for 'xx' would find
> 'xx' as it occurs in this file when what I need is for this test to fail as
> none of the records is an exact match to 'xx'.

> I can do a loop but was hoping there'd be a better way.

> Any suggestions would be appreciated.
> TIA.
> ken

by now you have seen many grep examples. as long as you ar not doing anything
look at sed and vi .

--
Bernie Chandler
http://www.nationwide.net/~bernie

 
 
 

1. How to match an Exact word in the file or using SED ?

Hi ,

I have a file(file.txt) with contents , say

"
Printing..CNT_COLUMN1 numerator ...  TCONGSAS+TCONGSAS_HR_OL  
Printing ...STORE_STR_N ...  TCONGSAS+TCONGSAS_HR_OL
"

Now I want replace the word "TCONGSAS" with "ERROR" .
When I tried I was able to replace "TCONGSAS" with "ERROR" , but the word
TCONGSAS_HR_OL is also changed to "ERROR_HR_OL" . This I want to avoid .
I want only to change the word "TCONGSAS" and not "TCONGSAS_HR_OL" .

Could any one tell me how to do this in vi editor and also using SED ,

Thanks in advance .

2. caching

3. using find and egrep and then print file name where match was found

4. Parallel Port

5. How to make egrep output the exact match instead of line?

6. Metro X

7. Substring match

8. "PCI BIOS passed nonexistant PCI bus 0"

9. substring extraction based on pattern matching

10. Case-insensitive matching of substring in bash ?

11. exact grep match

12. using grep for exact match

13. substring match