Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing lines based on multiple words 2

Status
Not open for further replies.

ddrillich

Technical User
Jun 11, 2003
546
0
0
US
Good Day,

I would like to exclude lines that have the word SECTOR as well as the word DREFIELD.

The following throws lines that have SECTOR but it mistakenly can remove lines with SECTOR that don't have DREFIELD -

Code:
 grep -v  SECTOR  < source.txt

Regards,
Dan


 
I would like to exclude lines that have the word SECTOR as well as the word DREFIELD.

"as well as" is ambiguous, so to be more precise.

Do you mean:
remove lines that contain BOTH words;
or
lines that contain either word.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Hi Chris,

I'm speaking about - remove lines that contain BOTH words.

Sorry about that...

Regards,
Dan
 
Hi

Not really a usual task for [tt]grep[/tt], but while there are only two terms, is easy enough :
Code:
grep -v 'SECTOR.*DREFIELD\|DREFIELD.*SECTOR' /input/file

But I recommend a more suitable tool, for example [tt]awk[/tt] :
Code:
awk '!/SECTOR/||!/DREFIELD/' /input/file

Adding a third term to the later one will be much easier.


Feherke.
feherke.ga
 
Okay, depending on the order of the words;

As grep uses a regular expression, the pattern will be

'word1.*word2'

.* == Any number of any characters.



Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Many thanks Chris and Feherke.

Regards,
Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top