Jozo
Jozo

Reputation: 115

Grep not catching the whole pattern

The pattern I want to get out is this: A city followed by a pointing arrow either direction ( -> or <- ) followed by another city and then followed by another pointing arrow.

I figured, maybe its because I didnt escape some of the character, but I tried escaping them, and didnt get the desired the result.

I run this command:

grep -o  '(\|stockholm\|paris\|tokyo){1}[<->]{2}(\|stockholm\|paris\|tokyo){1}[<->]{2}'

on text data like this :

tokyo->paris->$NILL1110102.8 fubar$`BLACKFri$$$SunBluePurpleOct011001Sun3.1415
Sun[<-->]0000103.14011010taoNILL$101101010001`3.14153$$$$$$phiparis->paris->
paris<-tokyo<-RedOrange
2.8VOID'$$[<-->]000010WHITEstockholm->stockholm->WHITE2.8taoSunWHITEphifooblah
FriNILLtao`paris<-tokyo<-WHITEVOIDNULLOrange101000phifubarOct$VOIDMon'[<-<-]$$ 
001100blah[<-<-]stockholm->paris->fubarfubar'FebYellow[<-->]NovNULL011001WHITE`$

The grep command ive written does indeed select the citys, but not the pointing arrows, which I wonder why and if someone can shed some light for me.

Thanks.

EDIT: Please dont come with diffrent solutions using sed or perl or whatever, I wish to learn my error here, thanks.

Output I get:

paris
paris
paris
stockholm
stockholm
paris
stockholm
paris

Desired output;

stockholm->stockholm->
paris<-tokyo<-
stockholm->paris->

Upvotes: 1

Views: 57

Answers (1)

tink
tink

Reputation: 15204

I think this does what you want?

grep -Eo '(stockholm|paris|tokyo){1}[<>-]{2}(stockholm|paris|tokyo){1}[<>-]{2}' jozo
tokyo->paris->
paris->paris->
paris<-tokyo<-
stockholm->stockholm->
paris<-tokyo<-
stockholm->paris->

Upvotes: 1

Related Questions