grep: do multiple searches?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep: do multiple searches?
# 1  
Old 03-14-2002
grep: do multiple searches?

I want to search the file /etc/passwd for all lines containing 'csh' but exlude all those lines that have '/usr' in them and dump the results into the file result.

IMPORTANT: I need to do this in one command line.

The following does not work:

grep -v \(\/usr\) \(csh\) /etc/passwd > result

What is the basic thing that I am doing incorrectly here?

do I need do first search for all instances of 'csh' and then direct this into another grep for exclude all instances of '/usr' ??

grep -v '/usr' /etc/passwd | grep csh > result


regards and thanks,

Steve
# 2  
Old 03-14-2002
Why does this have to be on one command line? This sounds like a homework exercise - and that is not what this forum is for.......(just so you know).

The suggestion you made will work.....have you tried it?

grep -v '/usr' /etc/passwd | grep csh > result
# 3  
Old 03-14-2002
It's a homework question...
I thought I'd responded to a similar question a little while ago, see here .
# 4  
Old 03-14-2002
yeah, I did figure this out on my own... that is why I put in the correct answer when I submitted the question.

I wasn't clear as to my real question or state of confusion...

I was struck by how simplistic these commands strings must be, and thought I was missing something.

When I tried to figure out the answer I just thought that there might be a way to put multiple arguments in one grep command instead passing the output onto another grep command.

Many years ago I did BLISS programming and I just have a lot of cobwebs stuck in my brain!
# 5  
Old 03-14-2002
Well you could always write a new grep command..... that does what I suggested does...

new-grep /usr csh > result.

Some more homework!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Customized text searches by using grep

I tried to ease text searches so made a customized grep: g () { if then i= for s in $2 do i="$i --include=*.$s" done else i='--include=*.txt --include=*.ini --include=*.*sh --include=*.c* --include=*.h --include=*.js --include=*.reg' fi grep -P -e \'$1\' -r "$i" } but I... (3 Replies)
Discussion started by: abdulbadii
3 Replies

2. UNIX for Dummies Questions & Answers

Output based on multiple searches

I have a file that looks like this: >Sample 539 GCCCAGCGCGCGILTGCCGCCGTCTCCGCCTGTCJOHNCCGCCATTGCCCCCGGTTAC I am using the following code to search specific patterns: awk '/^>/ { print $0 } NR==2 {if (/GIL/) { print "\t" "1" } else { print "\t" "0" }} NR==2 {if (/JOHN/) { print "\t""\t"... (7 Replies)
Discussion started by: Xterra
7 Replies

3. Shell Programming and Scripting

Need help in awk for multiple searches

I have a below file RCS File name : abc.txt something something .... symbolic names: implemented : 1.1 ssssssumthing Revision 1.2 date : 12/12/12 author : abc Revision 1.1 date : 11/11/11 author xyz So now , in this file i have to first look for the implemented... (1 Reply)
Discussion started by: ashishagg2005
1 Replies

4. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

5. Shell Programming and Scripting

Grep multiple strings in multiple files

Hi, every one! I have a file with multiple strings. file1 ATQRGNE ASQGVKFTE ASSQYRDRGGLET SPEQGARSDE ASSRDFTDT ASSYSGGYE ASSYTRLWNTGE ASQGHNTD PSLGGGNQPQH SLDRDSYNEQF I want to grep each string in hundreds of files in the same directory, further, I want to find out the string... (7 Replies)
Discussion started by: xshang
7 Replies

6. UNIX Desktop Questions & Answers

How do you [e]grep for multiple values within multiple files?

Hi I'm sure there's a way to do this, but I ran out of caffeine/talent before getting the answer in a long winded alternate way (don't ask ;) ) The task I was trying to do was scan a directory of files and show only files that contained 3 values: I940 5433309 2181 I tried many variations... (4 Replies)
Discussion started by: callumw
4 Replies

7. Shell Programming and Scripting

Nawk help searching for multiple lines and multiple searches

I use this command to find a search (Nr of active alarms are) and print one line before and 10 lines after the search keywords. nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=1 a=10 s="Nr of active alarms are:" *.log However, I would like to know how to tell it to print... (3 Replies)
Discussion started by: tthach830
3 Replies

8. UNIX for Dummies Questions & Answers

Grep multiple strings in multiple files using single command

Hi, I will use below command for grep single string ("osuser" is search string) ex: find . -type f | xarg grep -il osuser but i have one more string "v$session" here i want to grep in which file these two strings are present. any help is appreciated, Thanks in advance. Gagan (2 Replies)
Discussion started by: gagan4599
2 Replies

9. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

10. Shell Programming and Scripting

Creating searches?

Hello. Could do with some help on where to get started really. If anyone could help me it would be greatly appreciated. I have been working on this for a while now and I don't really know where to start but I am looking into creating a script that will process website hit files and output... (2 Replies)
Discussion started by: amatuer_lee_3
2 Replies
Login or Register to Ask a Question