How to check line existence in shell ?

Hi,

We have some config file and there we are looking to append a line if it is not found.

abc.conf
authpriv.*                                              /var/log/secure
mail.*                                                  -/var/log/maillog
*.debug  @vxhgt-hskhng02
cron.*                                                  /var/log/cron

Till now I have tried as below but no luck. Every time the line is appending to the file, even if the line is already present.
Note : there could be space or multiple tab between *.debug and @vxhgt-hskhng02. To handle the same I have used [:blank:] in my code.

PATTERN="*.debug[:blank:]@vxhgt-hskhng02" FILE=/etc/abc.conf if grep -q $PATTERN $FILE; then
echo "given pattern already found."

else
echo "pattern not found."
echo "*.debug  @vxhgt-hskhng02" >> /etc/abc.conf
fi

kindly suggest where I am going wrong here.

---------- Post updated at 04:09 PM ---------- Previous update was at 03:35 PM ----------

After searching in google....I got something like below

if grep -Fxq $PATTERN $FILE; 
then
echo "found"
else
echo "notfound"
fi

Still no luck.

Regards,
Litu

Try:

PATTERN= "\*\.debug[[:space:]]*\@vxhgt-hskhng02[[:space:]]*"

Thanks for quick reply.
I tried your way...but no luck. It is still appending the entry even if it already present there.

small modification to PATTERN makes it work:

PATTERN="*.debug[[:blank:]]*@vxhgt-hskhng02"
grep  $PATTERN file
*.debug  @vxhgt-hskhng02
1 Like

There is a typo in my last post. There should have been no space in the assignment operation.

# cat testIn
abc.conf
authpriv.*                                              /var/log/secure
mail.*                                                  -/var/log/maillog
*.debug  @vxhgt-hskhng02
cron.*                                                  /var/log/cron
# PATTERN="\*\.debug[[:space:]]*\@vxhgt-hskhng02[[:space:]]*"
# if grep -q $PATTERN testIn
> then
> echo "Pattern found"
> else
> echo "Pattern Not found"
> fi
Pattern found
1 Like

Try:

awk -v s='*.debug  @vxhgt-hskhng03' 'BEGIN{split(s,F)} 1; {for(i in F) if($i!=F) next; f=1} END{if(!f) print s}' file

:b: Thanks for your help. It worked out for me.:slight_smile:

---------- Post updated 06-17-14 at 11:38 AM ---------- Previous update was 06-16-14 at 07:51 PM ----------

I found one disconnect in my logic while appending the entry to the file.

If file conatins an entry for *debug @vxhgt-hskhng02 but it is commented, then we must append the entry to the file.

abc.conf

# *debug @vxhgt-hskhng02

In the above case we must append the entry.
Please suggest.

Try:

PATTERN='*.debug @vxhgt-hskhng02'
awk -v p="$PATTERN" 'BEGIN{split(p,F)} 1; {for(i in F) if($i!=F || $i~/^#/) next; f=1} END{if(!f) print p}' file

---
On Solaris use /usr/xpg4/bin/awk rather than awk

Hi Scrutinizer,

I appreciate your quick response.

As of now I have used grep command as mentioned below and it is working fine.

myfile
uucp,news.crit                                          /var/log/spooler
local7.*                                                /var/log/boot.log
*.debug         @vxhgt-hskhng02
#!/bin/bash
PATTERN="\*\.debug[[:blank:]]*\@vxhgt-hskhng02"
myfile=/etc/testfile
if grep -q $PATTERN $myfile
then
echo "pattern found"
else
echo "not found"
fi

In the same code I just want to add a logic to append the entry to the file, if the entry is present but it is commented.

We dont want to change the whole script again and use awk command. I hope you can understand mine point of view. Would you mind to suggest a solution that use grep command.

Regards,
Litu

I understand. How about:

PATTERN="^[[:blank:]]*\*\.debug[[:blank:]]*@vxhgt-hskhng02"
1 Like

While testing I found it is working. I have one doubt here ...how ^[[:blank:]]* is able read whether the line begins with # or not.

PATTERN="^[[:blank:]]*\*\.debug[[:blank:]]*\@vxhgt-hskhng02"

Can u just explain the command here.

Thanks,
Litu

It matches the pattern if preceded by only space ( [[:blank:]]* ) and only if that is at the beginning of the line ( ^ ). So if there is a comment character there, then there will be no match...

The old pattern that you were using wasn't right anyway, because if would have also matched something like tst*.debug @vxhgt-hskhng02 which is not right. Using ^[[:blank:]]* at the beginning fixes that as well (with the awk suggestion this precaution is not necessary since it uses string matching and discards leading space)..

Note that I removed the backslash before the @ since that is not necessary..

Thanks for your nice explanation and the way you handle and fix one more bug in my old logic. Appreciate your timely and quick response. I will have couple of tests again in our prod and will update thread.