Shell script: Checking if a particular string exists in a file (47 Views)
Reply
Occasional Advisor
Peter Remirez
Posts: 23
Registered: ‎05-30-2003
Message 1 of 13 (47 Views)
Accepted Solution

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.
Please use plain text.
Honored Contributor
Sridhar Bhaskarla
Posts: 6,350
Registered: ‎08-15-2001
Message 2 of 13 (47 Views)

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
Please use plain text.
Honored Contributor
Leif Halvarsson_2
Posts: 6,682
Registered: ‎01-09-2002
Message 3 of 13 (47 Views)

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
Please use plain text.
Respected Contributor
Donny Jekels
Posts: 612
Registered: ‎05-09-2002
Message 4 of 13 (47 Views)

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"
Please use plain text.
Esteemed Contributor
Caesar_3
Posts: 952
Registered: ‎06-05-2003
Message 5 of 13 (47 Views)

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
Please use plain text.
Trusted Contributor
Fragon
Posts: 373
Registered: ‎07-22-2002
Message 6 of 13 (47 Views)

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
Please use plain text.
Occasional Advisor
Peter Remirez
Posts: 23
Registered: ‎05-30-2003
Message 7 of 13 (47 Views)

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.

Please use plain text.
Trusted Contributor
Fragon
Posts: 373
Registered: ‎07-22-2002
Message 8 of 13 (47 Views)

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
Please use plain text.
Honored Contributor
Jean-Louis Phelix
Posts: 724
Registered: ‎10-07-2002
Message 9 of 13 (47 Views)

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 ...)
Please use plain text.
Frequent Advisor
Siddhartha M
Posts: 29
Registered: ‎02-08-2003
Message 10 of 13 (47 Views)

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
Please use plain text.
Honored Contributor
Leif Halvarsson_2
Posts: 6,682
Registered: ‎01-09-2002
Message 11 of 13 (47 Views)

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

Hi,

Compleating my previous example. The string is added to the beginning of the file if it not exists.

If ! grep -q "string" 2>/dev/null
then
echo "string" >tmpfile
cat >>tmpfile
mv tmpfile
fi
Please use plain text.
Frequent Advisor
Edgar Avila
Posts: 27
Registered: ‎03-06-2002
Message 12 of 13 (47 Views)

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

why don't you try:

#!/bin/sh
if grep "pglcls01" syslog.conf >> /dev/null; then
echo Here
else
echo "echo \"my string here\"" >> /home/file1
fi

or another way...

#!/bin/sh
y=`grep "pglcls01" syslog.conf|wc -l`

if [ $y -gt 1 ]; then
echo Here
else
echo "echo \"my string here\"" >> /home/file1
fi

Also, how what is the syntax to to check if a value is greater than or equal (>=) in SHELL script?
ans: -ge
Knowledge is power... so... give me the power!!
Please use plain text.
Esteemed Contributor
Caesar_3
Posts: 952
Registered: ‎06-05-2003
Message 13 of 13 (47 Views)

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

Hello!

Return to my option:

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

if ($status == 0) then
echo "String" > /tmp/temp.$$
cat LOGFILE >> /tmp/temp.$$
mv -f /tmp/temp.$$ LOGFILE
endif

Caesar
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation