Need Help to deal with empty string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need Help to deal with empty string
# 1  
Old 12-12-2008
Need Help to deal with empty string

Hi one to All

i have written the code for searching the string in the file for specified path. its working fine,

but my req:if the string is not available in the file , it should display the Message saying that , string is not available in the file.

code is:
echo "give the path where we can search the file and the string"
read path
echo -e "Give your inputs here, first filename, followed by the string with a space"
read -a file_string
echo "========================================================================"
echo -e "Here is your required output \n"
for i in `find "$path" -name "${file_string[0]}*"`;do cat $i | grep -i ${file_string[1]};done

Till now its working fine , but when the string is not available in the file it should display the message.

Thanks
Saic
# 2  
Old 12-12-2008
-q option of the grep
Code:
grep -q "search_string" filename

if [ $? -eq 0 ]
then
  echo "search string found"
else
  echo "search string not found"
fi

# 3  
Old 12-12-2008
What i'd do (and you may or may not be able to do this is) -
direct your "grep" to a file as well as to the standard output (do it twice)
and at the end of the "do", the following
if [ ! -s (whatever you called your redirect file) ]
then
echo "STRING NOT FOUND IN FILE"
fi
# 4  
Old 12-12-2008
Hi
i have tryed this but its giving error?

echo "give the path where we can search the file and the string"
read path
echo -e "Give your inputs here, first filename, followed by the string with a space"
read -a file_string
echo "========================================================================"
echo -e "Here is your required output \n"
for i in `find "$path" -name "${file_string[0]}*"`;do cat $i | grep -q ${file_string[1]};done
if [ $? -eq 0 ]
then
echo "search string found"
else
echo "search string not found"
fi
# 5  
Old 12-12-2008
Please post the error message that your are receiving. Also, please tell us which shell you are using. Bash? KSH?
# 6  
Old 12-12-2008
that should be within the for loop
Code:
for i in `<some_command>`
do
cat $i | grep -q "search_string"
if [ $? -eq 0 ]
then
echo "search_string found"
else
echo "search_string not found"
fi
done

# 7  
Old 12-12-2008
Hi matrixmadhan,

thanks for giving reply. iam using "Bash"

./srch0.sh
give the path where we can search the file and the string
/
-e Give your inputs here, first filename, followed by the string with a space
./srch0.sh[7]: read: bad option(s)
========================================================================
-e Here is your required output
grep: illegal option -- q
Usage: grep -hblcnsviw pattern file . . .
search string not found
grep: illegal option -- q


And the new code is :



echo "give the path where we can search the file and the string"
read path
echo -e "Give your inputs here, first filename, followed by the string with a space"
read -a file_string
echo "========================================================================"
echo -e "Here is your required output \n"
for i in `find "$path" -name "${file_string[0]}*"`
do
cat $i | grep -q ${file_string[1]}
if [ $? -eq 0 ]
then
echo "search string found"
else
echo "search string not found"
fi
done
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cant check empty string

Hello So i have that script collection, in which i have a single script to create a configuration file. In there, i have multiple occourences of something like this: prj_title=$(tui-read "What is the TITLE? ($prj_name):") ] && prj_title="${prj_name/_/ }" They all work as expected, if... (5 Replies)
Discussion started by: sea
5 Replies

2. Shell Programming and Scripting

How to check empty string in an XML tag?

I have an XML tag <abc> which is empty as <abc></abc>.If the the tag is empty I want to flag the file as bad. Please help. Thanks (3 Replies)
Discussion started by: aneeta13
3 Replies

3. Shell Programming and Scripting

Check if the string is empty

I am reading from a file and executing the jobs with/without parameters as the job requires. File job1 R job2 job3 Y 123 if then <job>.ksh else <job>.ksh $params fi This works fine if the line read from the file has parameters it executes like job1.ksh R But for... (2 Replies)
Discussion started by: nw2unx123
2 Replies

4. Programming

Checking not empty string

I have a string s Are the following equivalent? if ( ! s.empty() ) { } if ( s ) { } (1 Reply)
Discussion started by: kristinu
1 Replies

5. Shell Programming and Scripting

Perl : how to match non-empty string that has no spaces

Hi Everyone, I am looking for neat way to grep a non-empty string that basically contains a hostname, which might be in FWDN form or without the domain, for example: hostname.internal.domainname.net The file I am parsing contains blan lines (^$) and also series of "-" which in other places... (2 Replies)
Discussion started by: togr
2 Replies

6. Shell Programming and Scripting

Replace empty string on particular column

Hi I would like to replace empty string with a particluar value, any suggessions with awk ? my input file is not delimited with any delimiters input 52001073M8000000004567777 5200107 000000004567778 5200107 000000004567779 52001073M8000000004567789 Expected output... (5 Replies)
Discussion started by: selvankj
5 Replies

7. UNIX for Dummies Questions & Answers

Check to see if string var is empty

i have a veriable set var1 set var2 = abcd how can i check if var 1 is empty and if var 2 is not empty ??? (2 Replies)
Discussion started by: nirnir26
2 Replies

8. Shell Programming and Scripting

Initializing empty string with spaces

Hi, I want to Initialize a String with 50 spaces. I can do that by ex: Var1=" " But i dont want to do in this way? Is there any unix command where i can specify no of spaces to a varaible? like space(50) (1 Reply)
Discussion started by: Shiv_18
1 Replies

9. Shell Programming and Scripting

Check for empty string

Hello All, I have written shell script whcih at the max 3 parameters. When only one commandline argument and other two command line arguments are passed as empty string like eg : archive ' ' ' ' Then i need to check whether the commandline... (12 Replies)
Discussion started by: rahman_riyaz
12 Replies

10. Shell Programming and Scripting

How to check for null or empty string

Hi, I need to check for value not equal (<>) to 21 and not equal empty or null values. Please modify this script if then echo "$VALUE,$BSC_NAME,$BSC_ID" > $OUT_FILE/power_up.out end if TQ (5 Replies)
Discussion started by: doer
5 Replies
Login or Register to Ask a Question