简体   繁体   中英

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

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?

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

Given the input you posted all you need is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM