Unable to grep using wildcard in a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unable to grep using wildcard in a file.
# 1  
Old 12-05-2019
Unable to grep using wildcard in a file.

I wish to check if my file has a line that does not start with '#' and has
1. Listen and 2. 443

Code:
 echo "Listen               443" > test.out

grep 'Listen *443' test.out | grep -v '#'
Listen               443

The above worked fine but when the entry changes to the below the grep fails when i dont want it to fail.

Code:
echo "Listen     192.888.22.111:443" > test.out

Also, i do grep -v '#' to ignore lines having # i.e comments but is there a way to check if # is the starting (not necessary the first character considering there are whitespaces before the #) charecter in the line only then it should be ignored else be considered by the grep query.

So....

Quote:
# Listen 192.888.22.111:443
should not be considered

But

Quote:
Listen 192.888.22.111:443 #
should be considered.
# 2  
Old 12-05-2019
Would this do?
Code:
grep  '^[^#]*Listen.*[^0-9]443\([^0-9]\|$\)' file

# 3  
Old 12-06-2019
The problem is that your expression is matching Listen followed by any number of spaces then 443. Your definition is explicit in being for 'any number of spaces' with the * sequence.

Do you really mean "Match records that start with Listen and end with :443 #" ? That would be grep "^Listen.*:443 #$" or you could be more explicit and say that it would have to match an IP address structure with "^Listen.*([0-9]{1,3}\.){3}[0-9]{1,3}:443 #$"


To explain a little more:-
Code:
^                             - Start of line
Listen                        - Liternal text, exactly as it is.
.*                            - zero or more of any character
([0-9]{1,3}\.){3}             - exactly 3 of the bracketed bit (see line below and sub-note below)
[0-9]{1,3}                    - between 1 & 3 characters between 0 and 9 inclusive
:443 #                        - Literal text, exactly as it is.
$                             - End of line, important to avoid false matches that don't end at the end of your search..

So to expand more on ([0-9]{1,3}\.){3} the braces {3} mean three of the previous thing. In this case the thing is ([0-9]{1,3}\.) which is between 1 & 3 digits followed by an explicit dot/full stop. You have to use \. because the dot is special as you have in the .* which means any number of any character.

If the line might not start with Listen then you might just want to exclude a leading #, so you end up with the more complicated "^[^#]Listen.*([0-9]{1,3}\.){3}[0-9]{1,3}:443 #$" which is "Do not start with a hash. The [^#] means "Not this/these character(s)" which might be a bit confusing given it follows a ^ marking the start of the line. Of course if this might not be the first character, you might want to drop the first ^ anchoring it to the start of the line.




Does that help, or have I just confused things? Smilie

Robin

Last edited by hicksd8; 12-06-2019 at 11:48 AM..
This User Gave Thanks to rbatte1 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep with wildcard

hi anyone how can use grep with wildcard. for example grep "sample?txt" filename doesn't show sample1txt or grep "sample*txt" filename doesn't show sample123.txt that there is in filename. many thanks samad (12 Replies)
Discussion started by: abdossamad2003
12 Replies

2. Shell Programming and Scripting

Grep and BzGrep with Wildcard in Search Pattern

Hello All, I hope this is the right area. If not, Kindly let me know and I will report in the appropriate spot. I am needing to find a search pattern that will make the * act as Wildcard in the search pattern instead of being literal. The example I am using is bzgrep "to=<*@domain.com>"... (5 Replies)
Discussion started by: mancountry
5 Replies

3. UNIX for Dummies Questions & Answers

Wildcard in file names

I know this is very basic and looks strange to me . -bash-3.2$ wc -l *. Result: 51 test.bad Since my third range after dot is A-Z(upper), how it matched the d( Lower)? i was in an understanding that the above code would not fetch any result unless i have a file with 3 char extension... (4 Replies)
Discussion started by: saravana.sarak
4 Replies

4. Shell Programming and Scripting

Wildcard for grep

GNU grep with Oracle Linux 6.3 I want to grep for strings starting with the pattern ora and and having the words r2j in it. It should return the lines highlighted in red below. But , I think I am not using wildcard for multiple characters correctly. $ cat someText.txt ora_pmon_jcpprdvp1... (3 Replies)
Discussion started by: kraljic
3 Replies

5. Shell Programming and Scripting

Grep Wildcard search

How can i grep for a pattern with wildcard using grep? I want to identify all the lines that start with SAM and end in .PIPE IN.TXT SAM_HEADER.PIPE SAM_DETAIL.PIPE SAM_INVOICE.PIPE Can i do something like grep SAM*.PIPE IN.TXT (2 Replies)
Discussion started by: venky338
2 Replies

6. Shell Programming and Scripting

wildcard inside regular expression for grep

Hi, I'm on a Linux machine with a bash shell. I have some apache logs from where I want to extract the lines that match this pattern : "GET /dir1/dir2/dir3/bt_sx.gif HTTP/1.1" but where "/dir1/dir2/dir3/bt_sx" may vary , so I would like to grep something like cat apache.log | grep "\"GET... (2 Replies)
Discussion started by: black_fender
2 Replies

7. UNIX for Dummies Questions & Answers

Tail command with wildcard file name

Please help with the following command tail -f /appdata/logs/alert_audit517.txt | grep "Sep 02" The problem I have is with the file name "alert_audit517.txt". The 3 digit number at the end of the file name changes, so I need the file name to use a wildcard. Ive tried alert_audit***.txt, but... (5 Replies)
Discussion started by: robertson1995
5 Replies

8. Shell Programming and Scripting

Log file is not getting created & unable to grep error from it

Hi All, Below is my code,what I am trying to do is redirecting output of ftp to a log file & then greping the errors but here I am unable to grep "Permission denied" error only & also the corresponding log file is also not getting created. #!/bin/sh . cfg USER='abc' PASSWD='abc123' ... (4 Replies)
Discussion started by: ss_ss
4 Replies

9. Shell Programming and Scripting

Grep with wildcard in middle of word

How can grep G.*schema give me the result: ${Gacntg_dt}""'"' doesn't G.*schema say give me an unlimited number of characters between G and schema? :confused: (3 Replies)
Discussion started by: danmauer
3 Replies

10. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies
Login or Register to Ask a Question