How to search for string A OR string B using Grep?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to search for string A OR string B using Grep?
# 1  
Old 04-17-2011
How to search for string A OR string B using Grep?

Hi,

This is what I have done so far which is wrong for some reason:

Code:
grep -h "stringA | stringB" filename

Thank in advance!
# 2  
Old 04-17-2011
Try "egrep". Also if your comparison string does not include space characters be careful about extra space characters on the command line.

Code:
egrep -h "stringA|stringB" filename

This User Gave Thanks to methyl For This Post:
# 3  
Old 04-17-2011
Thanks for the swift reply. It works perfectly fine with egrep but is there a way that I can actually do it with grep because I want to copy the lines of a file in the ORGINAL ORDER to another file? ( Those which have string A or String B) in them.

Last edited by jboy; 04-17-2011 at 12:01 PM..
# 4  
Old 04-17-2011
egrep does not change the order.

Do not understand what you mean.

If you are trying to output to two different files, just use "grep" twice.
# 5  
Old 04-18-2011
Ok, here is what the question is asking:
Create a file called "filename.txt" containing, IN THE ORIGINAL ORDER, only the lines of

/usr/local/names.txt

starting with the string 'BAT' or 'SAT'. Ensure there is no extra white space in the file "names.txt".

I want to do this using grep not egrep.
Thanks.
# 6  
Old 04-18-2011
Try "grep -E" which is actually the same as "egrep".

Note the use of the circumflex character to mean "line starting with" in grep.

Code:
grep -E "^BAT|^SAT" /usr/local/names.txt >filenames.txt

I don't understand your "extra white space" bit. Needs an example.

If you wanted to remove all white space then try this in the pipeline:
Code:
tr -d '[:space:]'

This User Gave Thanks to methyl For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Search string or words in logs without using Grep

I'm in need of some kind of script that will search for a string in each logfile in a directory but we don't want to use GREP. GREP seems to use up to much of our memory causing the server to use up a lot of swap space. Our log files are bigger than 500M on a daily basis. We lately started... (8 Replies)
Discussion started by: senormarquez
8 Replies

3. Shell Programming and Scripting

Grep string search

Hi All, I'm using aix flavour unix where im trying for the below kind of pattern to search in all the files in a directory. I tried with different possible combinations like grep -ir "out.*\transaction_ctry_code" * etc.... Pattern I'm trying for : out<any thing>country_cd Here i... (0 Replies)
Discussion started by: rmkganesh
0 Replies

4. Shell Programming and Scripting

Search string within a file and list common words from the line having the search string

Hi, Need your help for this scripting issue I have. I am not really good at this, so seeking your help. I have a file looking similar to this: Hello, i am human and name=ABCD. How are you? Hello, i am human and name=PQRS. I am good. Hello, i am human and name=ABCD. Good bye. Hello, i... (12 Replies)
Discussion started by: royzlife
12 Replies

5. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

6. AIX

grep not working when search string has a space in it

Hi, I am trying to grep a string which has two words separated by space. I used a script to grep the string by reading the string in to a variable command i used in the script is echo "enter your string" read str grep $str <file> it is working fine when the entered string is a single... (3 Replies)
Discussion started by: sekhar gajjala
3 Replies

7. Shell Programming and Scripting

How to search (grep?) filename for a string and if it contains this then...

Hi i want to write a script that will search a filename e.g. test06abc.txt for a string and if it contains this string then set a variable equal to something: something like: var1=0 search <filename> for 06 if it contains 06 then var1=1 else var1=0 end if but in unix script :) (4 Replies)
Discussion started by: tuathan
4 Replies

8. UNIX for Dummies Questions & Answers

search excat string in another string (grep "fails")

hello, i have an statement which i have to correct because it shows the wrong result. i want to search an excat string in another string, command "grep" shows the wrong result: example: STRINGS="string1 string2 string3" search_string="string" incorrect: if then echo "not... (0 Replies)
Discussion started by: bora99
0 Replies

9. UNIX for Dummies Questions & Answers

grep for a search string

Hi, I want to grep one file for a search string and get the next 10 lines after the search string. for eg,in file tnsnames.ora,i have 100 entries.i want to search for one string and get the entry for that db. please help how to acheive this using awk or sed? Thanks. (11 Replies)
Discussion started by: raga
11 Replies

10. Shell Programming and Scripting

grep: RE error 41: No remembered search string.

Hi guys, I am currently facing a problem with my current script, the script basically monitor a list of process on the Unix system. If a process is down it will send a notification e-mail to me. Currently I am facing an error when running the script grep: RE error 41: No remembered search... (2 Replies)
Discussion started by: fkaba81
2 Replies
Login or Register to Ask a Question