[Solved] Having trouble with simple grep search


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Having trouble with simple grep search
# 1  
Old 03-13-2014
[Solved] Having trouble with simple grep search

I have a text file (allWords.txt), that I would like to search through. Here is a snippet of what it looks like...
Code:
a
aah
aahed
aahing
aahs
aardvark
aardvarks
aardwolf
ab
abaci
aback
abacus
abacuses
abaft
......

I would like to use the grep search to search, line by line, for all words that contain the letter "h" and the letter "a" and output the number of times that occurs in that text file. How would I go about doing that?

Last edited by vbe; 03-13-2014 at 12:36 PM..
# 2  
Old 03-13-2014
Like so:
Code:
grep -Ec "h.*a|a.*h" file
4

This User Gave Thanks to RudiC For This Post:
# 3  
Old 03-13-2014
Thank you very much!

What if I wanted to search words that do not have an "h" but do have an "a"?
# 4  
Old 03-13-2014
Code:
 grep -Ec a file

This User Gave Thanks to vbe For This Post:
# 5  
Old 03-13-2014
Also how would I count the words that contain at least one "h"?

grep -Ec "h" file

Is that right? Or am I just counting the total number of "h's" instead of counting the words that contain at least one "h"?

---------- Post updated at 11:26 AM ---------- Previous update was at 11:25 AM ----------

Quote:
Originally Posted by vbe
Code:
 grep -Ec a file

Wouldn't I need to put something in there to exclude words that have an "h"?
# 6  
Old 03-13-2014
the -c is for the count...
I suggest you test without and see for yourself!
To give you an idea:
Code:
$ grep -E h 0003.data
aah
haahed haahed
aahing
aahs
harward
$ grep -Ec h 0003.data
5

What do you notice with the count?
This User Gave Thanks to vbe For This Post:
# 7  
Old 03-13-2014
-c option will give counts for line which have match string, it is not counting the no of occurrence in each line.
This User Gave Thanks to sumitsks For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trouble with tail and grep

Good Morning, i ran into some trouble this morning while 'improving' my monitoring stuff. i would like to get a warning when the number of mails sent (outbound) by postfix is above a certain number. so far, so easy. to test that i simply put cat /var/log/mail.info | grep 'to=<' | grep -v -e... (1 Reply)
Discussion started by: Mike
1 Replies

2. UNIX for Beginners Questions & Answers

Grep trouble in Bash

PH=(6H 0 0 JD 9S 0 KD 0) #input from .txt file In the above array, I am trying to get the computer to tell me at what indices the non-zeros are at. I don't understand why this doesn't work... grep -v -b "0" ph.txt > position.txt Isn't grep -v supposed to show non matches in Bash?... (2 Replies)
Discussion started by: cogiz
2 Replies

3. Shell Programming and Scripting

Trouble with simple cutting, joining

Made a file called assignment1.sh and this is what's inside - #!/bin/bash $ cut -c 2 sales.txt > regionalsales.txt $ cut -c 6 sales.txt > regionalsales.txt $ cut -c 1 territory.txt > terrsummary.txt $ cut -c 2 territory.txt > terrsummary.txt $ cut -c 3 territory.txt > terrsummary.txt $ sort... (1 Reply)
Discussion started by: htzdruk
1 Replies

4. Shell Programming and Scripting

Having a little trouble with grep

I am trying to make a script that has grep finding lines that I get from the cat command, that start with something and end with an argument. I can get the first part, but whenever I try to add on the part that looks at the end of the line too, it stops working. Any ideas why? Here is my script: ... (11 Replies)
Discussion started by: sammythesp3rmy
11 Replies

5. Linux

[SOLVED] Trouble adding Tun module

So I have Ubuntu running on a sheevaplug develop kit, but the damn kernel thing doesn't come with /dev/tun enabled. It was suggested that I look into using module assistant to compile & get this set up but I'm having issues. Linux ubuntu 2.6.39.4 #2 PREEMPT Fri Aug 5 19:05:11 MDT 2011 armv5tel... (4 Replies)
Discussion started by: dpreviti
4 Replies

6. Shell Programming and Scripting

Emergency grep/delete trouble!!!

Hi everyone, thank you so much for reading. I built a user reference page from /etc/password for a class project. Now I need two shell scripts: 1) Add names to the reference page 2) Delete names from the reference page. I know grep -v is involved somehow and some piping but I am super stuck. ... (1 Reply)
Discussion started by: jsmpdx
1 Replies

7. UNIX for Dummies Questions & Answers

[Solved] Simple while loop does not exit

spath="/home/user/k/${1}" dpath="/home/user/seq/Nov17/${1}" cd $dpath ls -1 $spath > list c=1 while read list newlist=`echo $list | sed 's/.gz//' ` newnewlist=`echo $newlist | sed 's/.fastq//' ` do echo $c echo $list c=$(($c+1)) (6 Replies)
Discussion started by: analyst
6 Replies

8. Shell Programming and Scripting

[Solved] Strucked in Simple if

Whats the wrong with the following code: if ; then echo "File XYZ Exist" if ] ; then echo "File abc is not empty, script continues" else echo "File abc is empty, script aborts" exit 0 fi else echo "File XYZ does not exist" exit 0 Error: testscript.ksh:... (3 Replies)
Discussion started by: karumudi7
3 Replies

9. Shell Programming and Scripting

grep : having trouble with a single quote

Hi, I search for the string below which contains a single quote, some text '/home/myuser in the file myfile.txt as another user with the grep command as follows su - myuser -c "grep 'some text \'/home/myuser' myfile.txt" I also tried using two backslashes su - myuser... (6 Replies)
Discussion started by: cimcmahon
6 Replies

10. UNIX for Dummies Questions & Answers

Trouble with search and substitute

Hi there, I have a file with 1800+ lines and all are something like this: drwx--x--x 12960 4096 Oct 6 2006 palfvoet drwx--x--x 67205 4096 May 9 05:21 pallsopp drwx--x--x palmgren 4096 Oct 6 2006 palmgren now, as you... (3 Replies)
Discussion started by: besgal
3 Replies
Login or Register to Ask a Question