Egrep strings on different lines in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Egrep strings on different lines in file
# 1  
Old 08-06-2012
Egrep strings on different lines in file

Code:
test.txt:

appleboy
orangeletter
sweetdeal
catracer
conducivelot


I want to only grep out lines that contain "appleboy" AND "sweetdeal". however, the closest thing to this that i can think of is this:

Code:
cat test.txt | egrep "appleboy|sweetdeal"

problem is this only searches for all occurrences of "appleboy" OR "sweetdeal". not AND.

how can i do what i'm attempting to do? i think awk may be able to do this but i'm not sure.

OS = linux or redhat
shell = bash
# 2  
Old 08-06-2012
You get a Useless Use of Cat Award.

grep doesn't have an "and". grep isn't a programming language, it can't do arbitrary things, just a few built-in ones.

awk does have an "and". It is a programming language which can do whatever you tell it.

Code:
awk '/appleboy/ && /sweetdeal/' filename

# 3  
Old 08-06-2012
Quote:
Originally Posted by Corona688
You get a Useless Use of Cat Award.

grep doesn't have an "and". grep isn't a programming language, it can't do arbitrary things, just a few built-in ones.

awk does have an "and". It is a programming language which can do whatever you tell it.

Code:
awk '/appleboy/ && /sweetdeal/' filename

for some reason whem i tried your command it didn't work.

can you please show me how to modify the below so it actually shows the actual matching lines instead of the "found" or "not found":

Code:
awk '/appleboy/{ln1=NR} /sweetdeal/{ln2=NR} END{if(ln1 && ln2 && ln1 < ln2){print "found"}else{print "not found"}}' filename

# 4  
Old 08-08-2012
I have no idea what you're even trying to do there, so I can't say why it doesn't work.

My command works.

Code:
$ echo "appleboy" | awk '/appleboy/ && /sweetdeal/'
$ echo "sweetdeal" | awk '/appleboy/ && /sweetdeal/'
$ echo "sweetdeal something appleboy" | awk '/appleboy/ && /sweetdeal/'

sweetdeal something appleboy

$

What you asked for was lines matching both. If you actually want lines between them, I think that'd be

Code:
sed '/appleboy/,/sweetdeal/'


Last edited by Corona688; 08-08-2012 at 02:02 PM..
# 5  
Old 08-08-2012
Quote:
Originally Posted by Corona688
I have no idea what you're even trying to do there, so I can't say why it doesn't work.

My command works.

Code:
$ echo "appleboy" | awk '/appleboy/ && /sweetdeal/'
$ echo "sweetdeal" | awk '/appleboy/ && /sweetdeal/'
$ echo "sweetdeal something appleboy" | awk '/appleboy/ && /sweetdeal/'

sweetdeal something appleboy

$

What you asked for was lines matching both. If you actually want lines between them, I think that'd be

Code:
sed '/appleboy/,/sweetdeal/'

the problem is that, i guess your command works if the strings being searched for are on the same line.

but if they're on separate lines in a file, it doesn't work:

Code:
echo "sweetdeal" >> dinal
echo "something" >> dinal
echo "appleboy" >> dinal

cat dinal:

sweetdeal
something
appleboy


Code:
cat dinal | awk '/appleboy/ && /sweetdeal/'

gives nothing.

neither does this:
Code:
awk '/appleboy/ && /sweetdeal/' dinal

# 6  
Old 08-08-2012
not clear what you're after...

given foo.txt:
Code:
sweetdeal
something
nothing
appleboy
sweetdeal
foo
bar
bazz

What's the desired output?
# 7  
Old 08-08-2012
using your example,

i want to search for the words "sweetdeal" and "nothing".

if these two strings exist in the file, i want the entire line that they're found on to be outputted to the screen.

so in your example, i want something that would search for "sweetdeal" and "nothing", and when BOTH of these strings are found on different lines, I want them to be outputted like this:

Code:
awk '/sweetdeal/ && /nothing/' foo.txt:

sweetdeal
nothing

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

2. Shell Programming and Scripting

Exclude lines in a file with matches with multiple Strings using egrep

Hi I have a txt file and I would like to use egrep without using -v option to exclude the lines which matches with multiple Strings. Let's say I have some text in the txt file. The command should not fetch lines if they have strings something like CAT MAT DAT The command should fetch me... (4 Replies)
Discussion started by: Sathwik
4 Replies

3. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

4. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

5. UNIX for Dummies Questions & Answers

Editing strings within lines of file

Dear All, I have a file which contains a column with age, which is represented in two following patterns 1. "007/A" or ''007/a" or ''7 /a" ..... In this case A or a means year and I would like to extract only the numeric values eg 7 in the above case if this pattern exits in a line of file.... (5 Replies)
Discussion started by: pawannoel
5 Replies

6. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

7. Shell Programming and Scripting

Strings from one file which exactly match to the 1st column of other file and then print lines.

Hi, I have two files. 1st file has 1 column (huge file containing ~19200000 lines) and 2nd file has 2 columns (small file containing ~6000 lines). ################################# huge_file.txt a a ab b ################################## small_file.txt a 1.5 b 2.5 ab ... (4 Replies)
Discussion started by: AshwaniSharma09
4 Replies

8. Shell Programming and Scripting

Adding strings to lines in a file

Hi all, I have a positional text file that comes from some source application. Before it is processed by destination application I have to add some header (suffix) to every record(line) in the file. e.g. Actual File ............... AccountDetails AcNO Name Amount 1234 John 26578 5678... (3 Replies)
Discussion started by: sharath160
3 Replies

9. UNIX for Dummies Questions & Answers

Counting no of lines between two strings in a file

Hi all, I'm very very new to UNIX and AWK world.Please help me in finding a solution for my problem. I'm having a file like this ----------------------------------------------------------------- ~Version Information VERS. 2.0: CWLS log ASCII Standard -VERSION 2.0 WRAP. ... (4 Replies)
Discussion started by: santyshyam
4 Replies

10. Shell Programming and Scripting

Extracting the lines between 2 strings of a file

Hi, I have a sql file and i need to extract the table names used in the sql file using a unix script. If i can extract the lines between the keywords 'FROM' and 'WHERE' in the file, my job is done. can somebody tell me how to do this using a shell script. If u can just let me know, how to... (2 Replies)
Discussion started by: babloo
2 Replies
Login or Register to Ask a Question