Grep string start with . or whitespace

2.9k views Asked by At

I'm running a script that get an argument $1

I want to find all strings that start with either space or . before that argument. and end with either . or %
ex:

if $1 is Table1 then

1) "Table1"
2) " Table1"
3) ".Table1"
4) "Table1."
5) ".Table1."
6) " Table1."
7) ".Table1%"
8) " Table1%"

should be good

1

There are 1 answers

2
henfiber On BEST ANSWER
$ cat input_file.txt
Table1
 Table1
.Table1
Table1.
.Table1.
 Table1.
.Table1%
 Table1%
# the following should not match:
-Table1%
.Table1-
&Table1
Table10
myTable1

If by "finding all strings" you mean "whole lines", it is quite simple:

$ grep "^[. ]\?$1[.%]\?$" input_file.txt

If you mean matching strings occurring everywhere in a line, then it gets more complex since you have included the plain word "Table1" in your desired matches (match (1.) in your description). I would go with a 2-step approach.

First get all lines containing "Table1" as a word (grep -w).
Then filter out (grep -v) everything you don't want to be matched:

  $ grep -w "$1" input_file.txt |  
    grep -v "[^ .]\+$1" | 
    grep -v "$1[^.%]\+" 

or more compact:

 $ grep -w "$1" input_file.txt | grep -v "[^ .]\+$1\|$1[^.%]\+"  

result:

Table1
 Table1
.Table1
Table1.
.Table1.
 Table1.
.Table1%
 Table1%

grep -v "[^ .]\+$1" : means Filter-out (-v) previous-matches which start with a least one character other than "space" or "."

If you want to match any "whitespace" (e.g. tabs) and not only the common literal "space", replace the literal space " " in the grep patterns above with [:space:]