LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2012, 06:00 AM   #1
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Rep: Reputation: Disabled
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
 
Old 02-25-2012, 07:07 AM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
Old 02-26-2012, 07:24 AM   #3
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
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
 
Old 02-26-2012, 08:43 AM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by Micky12345 View Post
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.
 
Old 02-26-2012, 12:50 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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/

Last edited by David the H.; 02-26-2012 at 12:52 PM.
 
Old 02-26-2012, 06:46 PM   #6
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
BUT when i entered h* igot count as 10 but actually there's only 4 words starting with h

Can you tell me why
 
Old 02-27-2012, 04:48 AM   #7
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
All in one file? Is the result different, if you execute it on the command line?
 
Old 02-27-2012, 05:40 AM   #8
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
no all words are in the different files but in the current directory.
 
Old 02-27-2012, 09:19 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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"?

Last edited by millgates; 02-27-2012 at 09:21 AM.
 
Old 02-27-2012, 09:34 PM   #10
Micky12345
Member
 
Registered: Feb 2012
Posts: 58

Original Poster
Rep: Reputation: Disabled
THERE is only directory nd inside that 4 files are there with names file1, file2.......
 
Old 02-28-2012, 02:04 AM   #11
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 09:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 07:58 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration