Brenden Carvalho
Brenden Carvalho

Reputation: 127

extract text between two strings bash sed excluding

I am trying to extract some text between two strings (which appear only once in the file).

Suppose the file is,

....Some Data    
Your name is:

Dean/Winchester

You are male. Some data .....

I want to extract the text between 'Your name is:' and 'You are male.' both are unique and occur only once. So, the output should be,

Dean/Winchester

I tried using sed,

sed -n 's/Your name is:\(.*\)You are male./\1/' abcd

But it doesn’t output anything.

Any help will be appreciated. Thanks

Upvotes: 1

Views: 1323

Answers (2)

John1024
John1024

Reputation: 113994

$ sed -n '0,/Your name is/ d; /You are male/,$ d; /^$/d; p' abcd
Dean/Winchester

For variety, here is an awk solution:

$ awk '/Your name is/ {p=1; next} /You are male/ {exit} /^$/ {next} p==1 {print}' abcd 
Dean/Winchester

Upvotes: 3

slayedbylucifer
slayedbylucifer

Reputation: 23532

$ sed -n -e '/^Your name is:/,/^You are male/{ /^Your name is:/d; /^You are male/d; p; }' test 

Dean/Winchester

Upvotes: 3

Related Questions