LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2022, 06:26 PM   #1
Jason_25
Member
 
Registered: Nov 2001
Posts: 147

Rep: Reputation: 15
grep -w does not find the statement I am searching for


This does not work as expected. Nothing is returned with -w. If I remove the -w then stuff with port :22 will be found. But the problem is stuff can also be on port :2212345 and that will be caught. How do I filter just :22 ?

Code:
NOOK_CHECK_3=$(echo $NOOK | grep -w ":22 " | wc -l)

Last edited by Jason_25; 09-06-2022 at 06:28 PM.
 
Old 09-06-2022, 07:07 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 5,950
Blog Entries: 23

Rep: Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937Reputation: 3937
Try this as your grep expression:

Code:
grep ":22\b"
That looks for ":22" followed by a word boundary, either a non-word character or end of line.
 
1 members found this post helpful.
Old 09-06-2022, 08:51 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,944

Rep: Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168
As usual, if you show an example of the input and the desired output, it is always easier to assist.

My first thought would be that there is no boundary between the colon and the ip address
 
1 members found this post helpful.
Old 09-07-2022, 07:04 AM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,795

Rep: Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049

As per the Grep documentation:
Quote:
Originally Posted by https://www.gnu.org/software/grep/manual/html_node/Matching-Control.html#index-_002dw
-w
--word-regexp

Select only those lines containing matches that form whole words. The test is that the matching substring must either be at the beginning of the line, or preceded by a non-word constituent character. Similarly, it must be either at the end of the line or followed by a non-word constituent character. Word constituent characters are letters, digits, and the underscore. This option has no effect if -x is also specified.
Using -w means the space at the end of the pattern is likely redundant, but also that a port number is not going to work if there is a word character before the colon - as would be the case if the port is preceded by a valid hostname.


Removing the -w option and replacing the space with a \b will prevent longer ports matching and may be sufficient, but - depending on the specific input - might involve false positives (and thus have an incorrect check value and a bug in the script).

There's also several ways the check can probably be simplified/optimized, but not much point going there without example input and details of the context.

 
1 members found this post helpful.
Old 09-07-2022, 11:25 AM   #5
Jason_25
Member
 
Registered: Nov 2001
Posts: 147

Original Poster
Rep: Reputation: 15
Well it works now. I did try reading that manual page but my attention started fading after the word "Regex". I pretty much shutdown after that. I'm still not sure what you would use -w for other than if you wanted to spend all day cooped up in your office.

Anyway using \b works fine. I did try using it before because it is one of the most prominent solutions you find after searching. But bugs in my script were leading me to believe it was not working. $NOOK was getting cleared out before it was supposed to in short.

I don't know how it all works really. It is like magic that it can find :80 and :80. but not :8012345 which as we all know there are only like 65000 some ports available anyway. I mean someone obviously programmed it that way but it was well done.
 
Old 09-10-2022, 12:49 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,094

Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
The
Code:
grep ":22 "
is most precise, having real delimiting characters.

The
Code:
grep -w "22"
is a quick and dirty (and less precise) alternative.
It is identical with
Code:
grep "\b22\b"
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] return value for find or grep for use as an if statement condition IneptCoder Linux - Newbie 3 07-19-2013 10:45 AM
[SOLVED] how to use cp find and grep together to copy a list of files using find with grep babhijit Linux - Newbie 10 07-03-2013 12:25 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
searching within files. cat grep find w3bd3vil Linux - Newbie 4 10-13-2008 03:59 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 12:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration