LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 12-06-2003, 03:38 PM   #1
pcdebb
Member
 
Registered: Aug 2003
Location: Seffner, FL
Distribution: Mint
Posts: 82

Rep: Reputation: 15
using grep


don't know if this is the right area for this question, please pardon me if I'm misdirected.....

I'm writing a script that when i pass the parameter to it, it looks in a predetermined file and looks for a string. so far it works sortof. but if input part of a string it finds it but it should find it if i enter the whole string (for example, if i enter "pcd" it finds "pcdebb"). How can I limit it to find the whole string?

I also want to sort results if I have more than one result. would it be better if I redirect the results to a file then sort the file and then display the results?

The line of code I'm using:

grep -i "$1" [filename] || echo "no records found"

thanx for any help for a n00b
 
Old 12-06-2003, 03:48 PM   #2
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
If I had a file named somefile.txt, and inside it I had the words gdb gdba gdbc, and I only wanted to find gdb, I would use the following command:

grep -w 'gdb' somefile.txt

I hope that helps.

If you do a man grep, you'll find a lot of information on grep and regular expressions.


Last edited by oulevon; 12-06-2003 at 03:49 PM.
 
Old 12-06-2003, 03:50 PM   #3
vasudevadas
Member
 
Registered: Jul 2003
Location: Bedford, UK
Distribution: Slackware 11.0, LFS 6.1
Posts: 519

Rep: Reputation: 30
I reckon this is a linux - general question.
 
Old 12-06-2003, 03:51 PM   #4
pcdebb
Member
 
Registered: Aug 2003
Location: Seffner, FL
Distribution: Mint
Posts: 82

Original Poster
Rep: Reputation: 15
great, missed the -w option, works perfect, now to sorting.

am i having a mental problem if I say I like shell scripting a little bit?
 
Old 12-06-2003, 03:56 PM   #5
oulevon
Member
 
Registered: Feb 2001
Location: Boston, USA
Distribution: Slackware
Posts: 438

Rep: Reputation: 30
Quote:
Originally posted by pcdebb


am i having a mental problem if I say I like shell scripting a little bit?
I wouldn't say that. You can do tons of things through shell scripts if you know what you're doing. It's a good skill to have. I wish I did.
 
Old 12-06-2003, 03:58 PM   #6
pcdebb
Member
 
Registered: Aug 2003
Location: Seffner, FL
Distribution: Mint
Posts: 82

Original Poster
Rep: Reputation: 15
well I'll be taking a shell scripting class starting in January, and I have a book on it's way to me that I bought on amazon.com. it's really very interesting as I'm getting more into *nix
 
Old 12-08-2003, 09:31 AM   #7
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Quote:
Originally posted by pcdebb
...now to sorting.
Just pipe the output through the sort command:

Code:
grep -i "$1" [filename] | sort
 
Old 12-08-2003, 09:33 AM   #8
pcdebb
Member
 
Registered: Aug 2003
Location: Seffner, FL
Distribution: Mint
Posts: 82

Original Poster
Rep: Reputation: 15
ohhhh, that's a little easier than what i did. I basically piped the grep results to a file then sorted the file, but that's much easier!
 
Old 03-27-2004, 10:23 PM   #9
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
Hi, everyone,

since we on the topic of grep, can i ask so more questions on this?

I have a prob wif this prob, i ned to show words that begin wif a single 'z', and that is not adjacent to a vowel and this includes words starting and ending with 'z'.

i am able to get the words that do not have a vowel that is adjacent to 'z' but i cant get those words with a single 'z'. how do i do tat?

i tried grep 'z\{1\}' /usr/share/dict/words but i get no output. when i change to
grep 'z\{2\}' /usr/share/dict/words, i get those words with 2 'z' inside. is this the way to do it or is there another way?

if i get to do a single z, i would ned to find those words that do not have a vowel that is adjacent to 'z' which i did
grep '[^aeiouzAEIOUZ]z' /usr/share/dict/words | grep 'z[^aeiouzAEIOUZ]'
i am able to get the below ouput

blitzkrieg
Boltzmann
chutzpah
enzyme
Fitzgerald
Fitzpatrick
Fitzroy
frenzy
Holzman
Laszlo
Metzler
Nietzsche
Rosenzweig
Sulzberger

but this does not include the word waltz which fulfil the conditions as stated above, which ends with a 'z'.

can anyone help me?
 
Old 03-27-2004, 10:26 PM   #10
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
I feel like there was someone asking about this same homework assignment here a day or two ago
 
Old 03-27-2004, 10:52 PM   #11
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
Isit? Can direct me to that thread?
 
Old 03-27-2004, 11:00 PM   #12
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
yeah I looked around and I can't find the bugger. I shouldn't have given a spoiler on a homework question anyway.

You want to grep for \(either the beginning of a line OR a non-vowel, non-z\) z \(either the end of a line or a non-vowel, non-z\)
So if you look that stuff up in a page that describes regular expressions, you'll have it. I make the assumption that there is one line per word, as is the case in /usr/share/dict/words: otherwise you'll have to look for some other sort of word boundary instead.

Well I've gone this far, so I might as well say that ^ matches the beginning of a line, $ matches the end, and \| gives an "or" effect. I'd be an easy A as a teacher.
 
Old 03-27-2004, 11:22 PM   #13
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
Hi,

yes i do know abt '^', '$', '\|'. But my prob is

1) I cant get words that only contains a single 'z'. in which i tried the method mention in my previous post, and i could not get it. Is there any other way to achieve that aim?

2) /usr/share/dict/words is the path where I am supposed to look into to find the words

3) by doing this
grep '^z\|z$' /usr/share/dict/words

I am able to get words that start wif 'z' or end wif 'z'
but when i tried this

grep '^z\|z$' /usr/share/dict/words | grep [^aeiouzAEIOUZ]z\|z[^aeiouzAEIOUZ]

a combi of the above, no results is being shown.

What I said is that i can get words beginning or ending wif 'z', can get words that do not have a vowel that is adjacent to 'z' , but i cant get words that start with a single 'z' or combine all of them together.

i even tried this
egrep '^z|z$|[^aeiouzAEIOUZ]z|z[^aeiouzAEIOUZ]' /usr/share/dict/words

which gives me words such as

waltzes
waltzing
whiz
zonal
zonally
zone
zoned
zones
zoning
zoo
zoological
zoologically
zoom
zooms
zoos

which does not fulfill the conditions at all.

so far do you understand what i am having difficulties with?
 
Old 03-27-2004, 11:27 PM   #14
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Can you verify for me that this is the problem you want to solve?:

"Find all words which have a z which is not adjacent to any vowels or other z's"

I'm reasonably confident that I just gave you a solution to that. If that's not the problem, then I gave you the wrong answer : )
 
Old 03-27-2004, 11:41 PM   #15
xfiles_wolvie
Member
 
Registered: Mar 2004
Location: Singapore
Distribution: RH 9.0
Posts: 34

Rep: Reputation: 15
hi,

I am sorry for my poor english bu t I do not understnad the solution that you have given me. The exact questions is as follows

All words containing a single 'z' that is not adjacent to a vowel (this includes words ending or starrting with z)

Is your solution cater to this question?
 
  


Reply



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
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM
help on grep!!!! sanjith11 Programming 5 01-20-2005 05:43 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 01:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

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

All times are GMT -5. The time now is 12:43 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