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 |
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 |
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 |
Quote:
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' . Code:
grep -r '^\s*h' . Code:
grep -r '\bh' . |
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/ |
BUT when i entered h* igot count as 10 but actually there's only 4 words starting with h
Can you tell me why |
All in one file? Is the result different, if you execute it on the command line?
|
no all words are in the different files but in the current directory.
|
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"? |
THERE is only directory nd inside that 4 files are there with names file1, file2.......
|
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. |