Eliminating space constraint in grep

here in the below code just a space between 'Info' and '(' is showing that the patter doesnt match...

 echo "CREATE TABLE Info  (" | grep -i "CREATE TABLE Info  (" | wc | awk -F' ' '{print $1}'
1
echo "CREATE TABLE Info (" | grep -i "CREATE TABLE Info  (" | wc | awk -F' ' '{print $1}'
0

is there any way we could make it not to consider space as a difference...

echo "CREATE TABLE Info  (" | grep -i "CREATE TABLE Info[ ]*(" | wc -l

can i use this one too...?

echo "CREATE TABLE Info  (" | grep -i "[ ]*CREATE[ ]*TABLE[ ]*Info[ ]*(" | wc -l

what if the situation is like this

cat file1
.
.
.create table blah blah
.
.
create table info (
.
.
etc(many tables)

and

variable="CREATE TABLE Info   ("
how to make it match for this scenario...
cat file1 | grep -i $variable |wc -l  - - -- this wont work right 

echo "CREATE TABLE Info (" | grep -i "[ ]*CREATE[ ]*TABLE[ ]*Info[ ]*(" | wc -l --> It looks right syntactically. But since you're echo-ing just one line, there's not point in piping it to wc -l as you already know the line count is '1'.

Wrap $variable in double-quotes.

cat file1 | grep -i "$variable" |wc -l

(oh sorry i forgot to put quotes to the variable)but i meant the code wont work because of space parameter between 'info' and '(' in the variable... how to rectify that...? is there any way so that it will check whether that line is present or not in the file even thought there is variable spaces between the words in the variable?

$ cat input
CREATE   TABLE       Info         (
CREATE  TABLE  Info    (
CREATE    TABLE   Info    (
hellow world
hello earth
$ var="CREATE[ ]*TABLE[ ]*Info[ ]*("
$ cat input | grep -i "$var" | wc -l
3
1 Like

thanks this might work :slight_smile: i hope there is no other option to directly make them space insenstive by using some options or regular expressions (i was trying to avoid manipulating the variable content so)... thanks anyway :slight_smile:

You can cook a regex from the var on the fly:

grep -i "$(sed 's/ /[ ]*/g'<<< $var)" input
1 Like
 
grep -ic "create.*table.*info" input.txt
1 Like

thanks mirni the code works for me :slight_smile: :slight_smile: