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

a reliable way to check for exact string occurence in a text file 2

Status
Not open for further replies.

ogniemi

Technical User
Nov 7, 2003
1,041
PL

delimited by a space(s) or a tab(s).

eg. if in a line there is are:

u8-23.24.aaa 123.u8-23.24 u8-23.24.333

and want to check exactly for "u8-23.24" (in any field / line) I should get no match for above example line.

 
Hi

Should it be strictly delimited, or may be at the ends of the string ?
Code:
[gray]# strictly delimited[/gray]
grep '\su8-23.24\s' /path/to/input

[gray]# delimited or at the end of line[/gray]
grep '\(^\|\s\)u8-23.24\(\s\|$\)' /path/to/input


Feherke.
feherke.github.io
 
What about this ?
grep -E '(^|[[:space:]])u8-23\.24([[:space:]]|$)' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 

thank you. PHV's method worked in my case. is the way (PHV's modified a bit) with variable ok below?

Code:
$ echo "u8-23.24.aaa 123.u8-23.24 u8-23.24.333\nu8-23.24.aaa 123.u8-23.24 u8-23.24.333 u8-23.24 u8-23.24.333"|nl
     1  u8-23.24.aaa 123.u8-23.24 u8-23.24.333
     2  u8-23.24.aaa 123.u8-23.24 u8-23.24.333 u8-23.24 u8-23.24.333
$ echo "u8-23.24.aaa 123.u8-23.24 u8-23.24.333\nu8-23.24.aaa 123.u8-23.24 u8-23.24.333 u8-23.24 u8-23.24.333"|nl|grep '\su8-23.24\s'
$ echo "u8-23.24.aaa 123.u8-23.24 u8-23.24.333\nu8-23.24.aaa 123.u8-23.24 u8-23.24.333 u8-23.24 u8-23.24.333"|nl|grep '\(^\|\s\)u8-23.24\(\s\|$\)'
$ echo "u8-23.24.aaa 123.u8-23.24 u8-23.24.333\nu8-23.24.aaa 123.u8-23.24 u8-23.24.333 u8-23.24 u8-23.24.333"|nl|grep -E '(^|[[:space:]])u8-23\.24([[:space:]]|$)'
     2  u8-23.24.aaa 123.u8-23.24 u8-23.24.333 u8-23.24 u8-23.24.333
$ [red]A="u8-23.24"[/red]
$ echo "u8-23.24.aaa 123.u8-23.24 u8-23.24.333\nu8-23.24.aaa 123.u8-23.24 u8-23.24.333 u8-23.24 u8-23.24.333"|nl|grep -E '(^|[[:space:]])'[red]$A[/red]'([[:space:]]|$)'
     2  u8-23.24.aaa 123.u8-23.24 u8-23.24.333 u8-23.24 u8-23.24.333
$

 
I'd use this instead:
A="u8-23\.24"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top