Whether we can search multiple strings using or in grep -F


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Whether we can search multiple strings using or in grep -F
# 8  
Old 04-22-2013
Quote:
Originally Posted by hanson44
That's why I asked for test input. The problem statement is too vague. Smilie
With the comments provided by nanthagopal in messages #1 and #3 in this thread, I believe that we have a language barrier. I believe nanthagopal tried to answer the question joey asked but that the lack of proficiency in English made the answer ambiguous. I only see two ways to interpret the request, and since both possibilities are easy to code, I tried to provide both.

If neither of my interpretations was correct, I'll be happy to try again if nanthagopal can try explaining the problem better with sample input and corresponding expected output.
# 9  
Old 04-23-2013
Quote:
Originally Posted by hanson44
It would help greatly to give example test input, and expected output.

I hava a file named: file1.txt

I need to print the lines matching either the strings (string1 or string2)

for e.x,

using ordinary grep, we can do

Code:
grep "string1\|string2 file1.txt"


My question is,

How to grep the above case using grep -F


Note:

Code:
grep -F "string1\|string2" file1.txt

is not show any contents

Regards,
Nanthagopal A
# 10  
Old 04-23-2013
Here is one way to do it:
Code:
$ cat file1.txt
string1
string2
string3
string1 string2
string2 string1

Code:
$ cat test.sh
pattern=`echo -e "string1\nstring2"`
grep -F "$pattern" file1.txt

Code:
$ ./test.sh
string1
string2
string1 string2
string2 string1

# 11  
Old 04-24-2013
Quote:
Originally Posted by MadeInGermany
Code:
man grep
man fgrep

I think a newline separator works with all fgrep
Code:
grep -F "\
string1
string2"
"filename.txt"

This is string1 OR string2

Thank you for your reply,

I have used the above mentioned.

It's working good.

Regards,
Nanthagopal A
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search between two strings for multiple occurances

i search between two strings viz <app-deployment> & </app-deployment> and save the contents in a new file using the code snippet below. sed -n "/<app-deployment/,/<\/app-deployment>/p" deploy.tmp >found1.tmpBut if the search string apprears more than once in the file then how can i store the... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. 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

3. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Search multiple Strings in a File

Hi I want to search multiple strings in a file . But the search should start with "From" Keyword and end with before "Where" keyword. Please suggest me. Thanks (2 Replies)
Discussion started by: sboss
2 Replies

7. UNIX for Dummies Questions & Answers

Search for multiple strings and mail accordingly

Hi, I need to find if there is any error in the last few lines of the log file and send a mail accordingly.For example, Following errors can be logged in the log file. ERR_1="DB Connection not established" ERR_2="Server Unloading" I need to find if these errors are found in the log file and... (3 Replies)
Discussion started by: AnneAnne
3 Replies

8. Shell Programming and Scripting

How to search multiple strings in a file

Hi All, I want to search all the ksh scripts that has following details. 1. Search for "exit 0" 2. Search for "sqlldr" or sqlplus" 3. In the above files i want to search for all the script that has no "case" in it. Please advice. Thanks, Deep (2 Replies)
Discussion started by: deepakpv
2 Replies

9. Shell Programming and Scripting

grep: outputting search strings

Newbie question -- any help very much appreciated: I want to be able to get grep (or whatever else would work) to return not only matching lines, but also the original input string: An example may help: Suppose I have two files data1.txt and data2.txt: data1.txt Hello my name is foo. What... (3 Replies)
Discussion started by: tapmas
3 Replies

10. Shell Programming and Scripting

Problem to search multiple strings

Hi all, I am search a string from a file using following command, I want to pick the message ( I.e print $5) from the lookup file if and only if both $hostname and $instancename match. Message=`cat $lookup_tbl| awk '/'$category'/ {if ('$hostname' == '$2' || '$instancename' == '$3') print $5}'`... (2 Replies)
Discussion started by: sudhish
2 Replies
Login or Register to Ask a Question