如何使用sed或awk在开始子字符串和结束子字符串的基础上提取行部分

Mia*_*mad 1 linux awk sed data-analysis

我有一个文本没有空格的多行文件.

Thereisacat;whichisverycute.Thereisadog;whichisverycute.
Thereisacat;whichisverycute.Thereisadog;whichisverycute.
Run Code Online (Sandbox Code Playgroud)

我想提取猫和可爱之间的字符串(第一次出现不是第二次),即输出

;whichisvery
;whichisvery
Run Code Online (Sandbox Code Playgroud)

我接近得到它,但我最终得到了从猫到最后可爱的字符串从这里的命令.

sed -e 's/.*cat\(.*\)cute.*/\1/'
Run Code Online (Sandbox Code Playgroud)

我正进入(状态

;whichisverycute.Thereisadog;whichisvery
;whichisverycute.Thereisadog;whichisvery
Run Code Online (Sandbox Code Playgroud)

我如何从cat到第一次出现可爱的文字而不是最后?

Rav*_*h13 5

编辑:因为我投票,我已经给出了解决方案awk,我无法理解为什么.因此,添加一个解决方案sed(尝试如果可以删除投票).

sed 's/cute.*//;s/.*cat//' Input_file
Run Code Online (Sandbox Code Playgroud)

你可以尝试一下,让我知道这是否对你有帮助.

awk '{sub(/cute.*/,"");sub(/^.*cat/,"");print}'  Input_file
Run Code Online (Sandbox Code Playgroud)