ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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)
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.
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:
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"?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.