Piping GREP


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Piping GREP
# 1  
Old 01-29-2009
Piping GREP

Hi,

I need to use a double grep so to speak. I need to grep for a particular item say BOB and then for each successful result I need to grep for another item say SMITH.

I tried grep "BOB" filename | grep "SMITH"

but it does not seem to work.

I can achieve my desired result using an intermediate temporary file but I am trying to avoid that. Is there someway to set up a piping assembly line? I looked at the MAN pages but did not find that struck me as useful but I am new to UNIX so I thought I would ask here...
# 2  
Old 01-29-2009
Code:
$ echo 'BOB SMITH' | grep BOB | grep SMITH
BOB SMITH
$

There's no reason it shouldn't work. Could you post some data from the input file, and the version of yours that didn't work?
# 3  
Old 01-30-2009
That works

Okay that works no problem

But supposing Bob Smith was in a file. I need to grep for Bob in a file and then grep again to make sure it contains Smith. The two are not side by side so I have to do two greps....

Should I be using fgrep? If so how?
# 4  
Old 01-30-2009
Code:
$ echo 'BOB SMITH' > file
$ grep BOB < file | grep SMITH
BOB SMITH
$

I repeat:

There's no reason your first version shouldn't work. Could you post some data from the input file, and the version of yours that didn't work?
# 5  
Old 01-30-2009
Quote:
Originally Posted by Corona688
Code:
$ echo 'BOB SMITH' > file
$ grep BOB < file | grep SMITH
BOB SMITH
$

I repeat:

There's no reason your first version shouldn't work. Could you post some data from the input file, and the version of yours that didn't work?
GNU grep 2.5.3

Copyright (C) 1988, 1992-2002, 2004, 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
---------
What you posted above works but I have some questions/concerns:

1) What is the "<" character for? When I tried out your code I left it out and the code executed fine so I am wondering if it is really necessary.
2) When I create a file and put BOB on one line and SMITH on the other your code fails. Why? It is this type of situation I have problems with.
# 6  
Old 01-30-2009
Question

grep checks by line, not by file.

Is your example of Bob on one line and Smith on another what you are trying to accomplish? Merely to see if two things exist inside a file anywhere?
# 7  
Old 01-30-2009
Hammer & Screwdriver Would this do what you want?

Code:
> cat file158
BOB
TED
HENRY
SMITH

> RESULT=`tr -d "\n" <file158 | grep "BOB" | grep "SMITH" | tr "[:print:]" "Y" | tr -s "Y"`
> echo $RESULT
Y

> RESULT=`tr -d "\n" <file158 | grep "BOB" | grep "SMITY" | tr "[:print:]" "Y" | tr -s "Y"`
> echo $RESULT

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

Piping to grep with pbpaste

cat file 1 aaa 2 bbb 3 ccc 4 ddd In TextEdit, I then copy the characters “ccc” to the clipboard. The problem is that the following command gives no output: bash-3.2$ pbpaste | grep - file Desired output: 3 ccc What should the syntax be for that command? I am using MacOS El... (3 Replies)
Discussion started by: palex
3 Replies

2. Shell Programming and Scripting

Piping through grep/awk prevents file write

So, this is weird... I'm running this command: iotop -o -P -k -bt -d 5 I'd like to save the output relelvant to rsyslogd to a file, so I do this: iotop -o -P -k -bt -d 5 | grep rsyslogd >> /var/log/rsyslogd Nothing is written to the file! I can write the full output to the file: ... (2 Replies)
Discussion started by: treesloth
2 Replies

3. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

4. Shell Programming and Scripting

piping from grep to awk without intermediate files

I am trying to extract the file names alone, for example "TVLI_STATS_NRT_XLSTWS03_20120215_132629.csv", from below output which was given by the grep. sam:/data/log: grep "C10_Subscribe.000|subscribe|newfile|" PDEWG511_TVLI_JOB_STATS.ksh.201202* Output: ... (6 Replies)
Discussion started by: siteregsam
6 Replies

5. Ubuntu

Piping with grep

Hi everybody, I have a big file with blast results (if you know what this means, otherwise look at it just as a text file with a specific form). I am trying to extract some ids from within this file, which have certain parameters. For example, some Of my IDs have the term 'No hit results'... (6 Replies)
Discussion started by: frymor
6 Replies

6. Ubuntu

Piping with grep

Hi everybody, I have a big file with blast results (if you know what this means, otherwise look at it just as a text file with a specific form). I am trying to extract some ids from within this file, which have certain parameters. For example, some Of my IDs have the term 'No hit results'... (1 Reply)
Discussion started by: frymor
1 Replies

7. Shell Programming and Scripting

Piping STDOUT as pattern to grep or sed

$>cat file.txt 123 d3 234 abc 3 zyf 23 124 def 8 ghi kz0 ... ... I have the following output on the screen through <some command>. $> <some command> abc def ghi ... ... I have to search for each of these patterns in the file.txt and print the lines in file.txt matching the... (4 Replies)
Discussion started by: VNR
4 Replies

8. Shell Programming and Scripting

Grep causing long delay (batching) whilst piping

Hi all. I have a problem at work which I have managed to break down into a simple test scenario: I have written a monitoring script that outputs every second the status of various processes, but for now, lets just print the date input.sh: while true do date sleep 1 done This... (9 Replies)
Discussion started by: spudtheimpaler
9 Replies

9. Shell Programming and Scripting

question about grep, cut, and piping

Howdy folks, I am fairly new to scripting but have lost of expirience in c++, pascal, and a few other. I am trying to complete a file search script that is sent a file name containing data to search that is arranged like this "id","name","rating" "1","bob","7" etc and an argument to... (1 Reply)
Discussion started by: dyrt
1 Replies

10. UNIX for Dummies Questions & Answers

piping the output of find command to grep

Hi, I did not understand why the following did not work out as I expected: find . -name "pqp.txt" | grep -v "Permission" I thought I would be able to catch whichever paths containing my pqp.txt file without receiving the display of messages such as "find: cannot access... Permisson... (1 Reply)
Discussion started by: 435 Gavea
1 Replies
Login or Register to Ask a Question