Wednesday, October 26, 2022

[SOLVED] how to extract line portion on the basis of start substring and end substring using sed or awk

Issue

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?


Solution

Given the input you posted all you need is:

$ awk -F'cat|cute' '{print $2}' file
;whichisvery
;whichisvery


Answered By - Ed Morton
Answer Checked By - Pedro (WPSolving Volunteer)