2

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?

miken32
  • 42,008
  • 16
  • 111
  • 154
Dr. Mian
  • 3,334
  • 10
  • 45
  • 69

2 Answers2

4

In sed:

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

In awk:

awk '{sub(/cute.*/,"");sub(/^.*cat/,"");print}'  Input_file
miken32
  • 42,008
  • 16
  • 111
  • 154
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
4

Given the input you posted all you need is:

$ awk -F'cat|cute' '{print $2}' file
;whichisvery
;whichisvery
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 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. – Dr. Mian 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. – Dr. Mian 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