LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   shell script (https://www.linuxquestions.org/questions/programming-9/shell-script-931198/)

Micky12345 02-25-2012 06:00 AM

shell script
 
shell script to search for a single word pattern arecursively in the current directory and displays no. of times it occured.


I wrote a shell script

echo "Enter a word pattern"
read pattern
count=`grep -wr $pattern. 2>/dv/null | wc -l
echo $pattern

nd when i execute the shell script
i got:
Enter a word pattern
I entered hello
Ans was 9
I got it correctly
but when i entered like h*(all starting with h) i got wrong answer

jlinkels 02-25-2012 07:07 AM

You didn't copy & paste your code. Hence there are so many errors in the text you typed the script wouldn't ever run. Please copy & paste your code. From what you posted it isn't clear whether 'h*' is the file name or the search pattern.

The most likely reason why 'h*' doesn't work is because the asterisk has special meaning in the shell. When calling your script, you should enclose 'h*' in single quotes. In your script, use "$pattern" (double quotes included)

jlinkels

Micky12345 02-26-2012 07:24 AM

MY PROGRM IS BELOW:
echo "Enter a pattern"
read pattern
count=`grep -wr $pattern . 2>/dev/null | wc -l`
echo $count


While executing this shell script
when i gave pattern as hello i got correct answer


But when i entered as h*(for all string starting with h)
i got wrong answer

millgates 02-26-2012 08:43 AM

Quote:

Originally Posted by Micky12345 (Post 4612345)
MY PROGRM IS BELOW:
echo "Enter a pattern"
read pattern
count=`grep -wr $pattern . 2>/dev/null | wc -l`
echo $count


While executing this shell script
when i gave pattern as hello i got correct answer


But when i entered as h*(for all string starting with h)
i got wrong answer

I tried your script. If I enter h* as a pattern, it outputs everything in the current directory. Which is to be expected.
grep -wr 'h*' . means "match each string containing a word consisting of zero or more consequent h characters". If you want lines that start with h, the regex would be

Code:

grep -r '^h' .
If you want to allow initial spaces, then

Code:

grep -r '^\s*h' .
If you want strings that contain a word starting with h, it's

Code:

grep -r '\bh' .
and so on. I suggest that you read some regex tutorials for more details.

David the H. 02-26-2012 12:50 PM

Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.

Then take the time to read through a good tutorial or two, so that you can get the basic concepts right. I particularly recommend this one, along with the related faq and pitfall pages:

http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls

You can follow links from there to other helpful pages as well.


Here are some other useful bash links:
http://www.linuxcommand.org/index.php
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
http://ss64.com/bash/

Micky12345 02-26-2012 06:46 PM

BUT when i entered h* igot count as 10 but actually there's only 4 words starting with h

Can you tell me why

Reuti 02-27-2012 04:48 AM

All in one file? Is the result different, if you execute it on the command line?

Micky12345 02-27-2012 05:40 AM

no all words are in the different files but in the current directory.

millgates 02-27-2012 09:19 AM

And how many lines do the files in the current directory have in total? Isn't it 10, by chance?
Are there any files in the current directory, which name starts with "h"?

Micky12345 02-27-2012 09:34 PM

THERE is only directory nd inside that 4 files are there with names file1, file2.......

millgates 02-28-2012 02:04 AM

ok, I have already implied why your search does not work the way you expect it to, but just for the case I'll say it again:

h* is *not* the correct pattern to match words starting with h.


All times are GMT -5. The time now is 03:57 PM.