Operating System - HP-UX
1748219 Members
4775 Online
108759 Solutions
New Discussion юеВ

Re: Shell script: Checking if a particular string exists in a file

 
SOLVED
Go to solution
Peter Remirez
Occasional Advisor

Shell script: Checking if a particular string exists in a file

HI,
I would like to check if a particular string exists in a file, and if it doesn't, then insert that string in the file.

I was wondering if anyone could show me how I could perform such checks in a SHELL script?

I tried the following method below but it did not work:

#!/bin/sh
if [grep "pglcls01" syslog.conf|wc -l gt 1];then
echo Here
else
echo "echo "my string here" >> /home/file1
fi

duran% ./sys-1
./sys-1: [grep: command not found
wc: gt: No such file or directory
wc: 1]: No such file or directory
0 total
duran% ls -l syslog*
-rw-r----- 1 premir eng 390 Jun 17 17:38 syslog.conf

Also, how what is the syntax to to check if a value is greater than or equal (>=) in SHELL script?

Could some kindly help me out?

Thanks in advance.
12 REPLIES 12
Sridhar Bhaskarla
Honored Contributor
Solution

Re: Shell script: Checking if a particular string exists in a file

Hi Peter,

There are syntax errors in your script. I modified it to work.

#!/bin/sh
if [ $(grep "pglcls01" syslog.conf|wc -l) -gt 1 ]
then
echo Here
else
echo "echo "my string here"" >> /home/file1
fi

$(commmand) will evaluate the command. You could also use the back ticks (`grep pglcls01 syslog.conf`).

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Leif Halvarsson_2
Honored Contributor

Re: Shell script: Checking if a particular string exists in a file

Hi,
If you just want to check if a string exist in a file you can use the return code:

If grep >/dev/null 2>&1
then
echo "String exist"
else
echo "String don't exist"
fi
Donny Jekels
Respected Contributor

Re: Shell script: Checking if a particular string exists in a file

Is there a spesific area where you want to add the string.

At the end of the file?
In the beginning of the file?
Somewhere in the middle?
In the beginning of the line or at the end of the line?

finding if a string of text exists is easy. placing new text in a flat file is also very straitforward, just let me know the answers to these questions and I'll write you a routine - do you prefer perl or shell scripts?
"Vision, is the art of seeing the invisible"
Caesar_3
Esteemed Contributor

Re: Shell script: Checking if a particular string exists in a file

Hello!

Try the next in csh:

grep -q "String" >& /dev/null

if ($status == 0) then
echo "String exist"
else
echo "String" >>
endif

Caesar
Fragon
Trusted Contributor

Re: Shell script: Checking if a particular string exists in a file

Hi,just a simple way to check if a string exists in a file:

if grep "String" file >/dev/null 2>&1
then
echo "String exists!"
else
echo "String" >>file
fi

To check if a value is greater than or equal, you can use:
if [ ! $a -lt 10 ]

Good luck!
-ux
Peter Remirez
Occasional Advisor

Re: Shell script: Checking if a particular string exists in a file

Donny,
I would like to have the string preferably at the beginning of the file.

If possible, would like to have the script to written in SHELL.

Thanks very much.

Fragon
Trusted Contributor

Re: Shell script: Checking if a particular string exists in a file

Just for text file:

if ...
then
...
else
echo "String" >$$.tmp
cat file >>$$.tmp
cat $$.tmp >file
rm $$.tmp
fi

Good luck!
-ux
Jean-Louis Phelix
Honored Contributor

Re: Shell script: Checking if a particular string exists in a file

Hi,

So it could b :

if grep -q "pglcls01" syslog.conf
then
echo Here
else
(
echo "Mystring"
cat /home/file1
) > /tmp/tmp$$
mv /home/file1 /home/file1.prev
mv /tmp/tmp$$ /home/file1
fi

Regards.
It works for me (┬й Bill McNAMARA ...)
Siddhartha M
Frequent Advisor

Re: Shell script: Checking if a particular string exists in a file

#!/bin/sh
CHK=`cat /etc/syslog.conf | grep pglcls01| wc -l`
if [ $CHK != 0 ]
then
echo "required string exists"
else
echo pglcls01 >> /etc/syslog.conf
fi