-1

How do I extract text in between strings first instance only.

Source:

test66="keyword1"y67h6y7 test66="keyword2"hj67h67j
test66="keyword3"f54fd543f456

output:

keyword1
barway
  • 75
  • 1
  • 1
  • 5
  • It's not very clear what you mean with "between strings". Try to improve the explanation as well as give some of your attempts. – fedorqui Oct 27 '14 at 10:10

4 Answers4

1
sed -n '/test66="\(.*\)"/ {s//\1/p; q}' source.txt

Inspired by: https://stackoverflow.com/a/17086464/4323

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1
grep -m 1 -oP 'test66="\K[^"]*'

Output:

keyword1
Cyrus
  • 84,225
  • 14
  • 89
  • 153
0

awk way
Works for your updated requirements

awk '{x=match($0,/test66="([^"]+)"/,a)}x{print a[1];exit}' file

sed way

sed -n '/test66="\([^"]\+\)".*/{s//\1/p;q}' file

They both do pretty much the exact same thing.

1.Search for string

2.Store value inbetween quotes.

3.Print that value

4.Quit

0

This might work for you (GNU sed):

sed -r '/test66="/{s//\n/;s/^[^\n]*\n([^"]*)".*/\1/;q}' file

Replace the required pattern with a marker i.e. \n and remove strings either side of the marker, print and quit.

potong
  • 55,640
  • 6
  • 51
  • 83