如何使用sed或awk在开始子字符串和结束子字符串的基础上提取行部分 [英] how to extract line portion on the basis of start substring and end substring using sed or awk

查看:240
本文介绍了如何使用sed或awk在开始子字符串和结束子字符串的基础上提取行部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a multiline file with text having no spaces.

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

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

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/'

我得到

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

如何将猫的文字显示为可爱而不是最后出现的文字?

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

推荐答案

鉴于您发布的所有输入内容是:

Given the input you posted all you need is:

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

这篇关于如何使用sed或awk在开始子字符串和结束子字符串的基础上提取行部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆