1

I have a multiline file with text having no spaces.

Thereisacat;whichisverycute.Thereisadog;whichisverycute.
Thereisacat;whichisverycute.Thereisadog;whichisverycute.

I want to extract string between cat and cute (first occurrence not second) that is the output is

;whichisvery
;whichisvery

I am close to getting it but I end up getting string from cat to the last cute with the command from here.

sed -e 's/.*cat\(.*\)cute.*/\1/'

I am getting

;whichisverycute.Thereisadog;whichisvery
;whichisverycute.Thereisadog;whichisvery

How can I get the text from cat to the first occurrence of cute not last?

Mian Asbat Ahmad
  • 2,948
  • 9
  • 38
  • 63

2 Answers2

5

EDIT: Since I got down vote that I have given solution in awk which I couldn't understand why. So adding a solution in sed(trying if down vote could be removed).

sed 's/cute.*//;s/.*cat//' Input_file

Could you please try following and let me know if this helps you.

awk '{sub(/cute.*/,"");sub(/^.*cat/,"");print}'  Input_file
RavinderSingh13
  • 117,272
  • 11
  • 49
  • 86
3

Given the input you posted all you need is:

$ awk -F'cat|cute' '{print $2}' file
;whichisvery
;whichisvery
Ed Morton
  • 172,331
  • 17
  • 70
  • 167
  • This one works better on my real file. For some reason the first answer is doing great on above example but not working on my real file. I think this answer is handling the complex string in a better way. Please try similar answer in sed too and thanks for the answer. – Mian Asbat Ahmad Jun 26 '18 at 18:35
  • What does `Please try similar answer in sed` mean? There is no similar sed answer. – Ed Morton Jun 26 '18 at 18:39
  • hmm okay cool. I thought this problem could be solved in many ways. – Mian Asbat Ahmad Jun 27 '18 at 04:23
  • 1
    The problem can be solved in many ways, as you can see in the various answers you got, but it can't be solved the same way in all tools as different tools have different functionalities. – Ed Morton Jun 27 '18 at 04:33