Paul
Paul

Reputation: 1117

Delete only the patterns and not the entire line

I have a text file as below

 OG1: Panda|Koala_1.2_animals Panda|Koala_extinct Elephant|Ostrich_1_0_mammal_bird
 OG2: Panda|Koala_1.2_animals Panda|Koala_cute Oyster|Ostrich_1_0_seafood_bird
 OG3: Panda|Koala_animals Platypus|Hummingbird_1_0_mammal_bird
 OG4: Platypus|Hummingbird_1_0_mammal_bird

I want to remove any word that starts with Panda|Koala until the space how many ever times the pattern appears to get

 OG1: Elephant|Ostrich_1_0_mammal_bird
 OG2: Oyster|Ostrich_1_0_seafood_bird
 OG3: Platypus|Hummingbird_1_0_mammal_bird
 OG4: Platypus|Hummingbird_1_0_mammal_bird

I tried using

sed'/Panda|Koala/d' data.txt

This deletes the entire line which contains the pattern

Upvotes: 0

Views: 54

Answers (2)

Claes Wikner
Claes Wikner

Reputation: 1517

awk '{print " "$1,$NF}' file

 OG1: Elephant|Ostrich_1_0_mammal_bird
 OG2: Oyster|Ostrich_1_0_seafood_bird
 OG3: Platypus|Hummingbird_1_0_mammal_bird
 OG4: Platypus|Hummingbird_1_0_mammal_bird

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89629

You need to replace with an empty string, use the replacement command s/pattern/replacement/:

sed 's/Panda|Koala[^ ]* *//g' data.txt

[^ ] means a character that isn't a space and g performs the replacement for all occurrences in the line.

Upvotes: 2

Related Questions