两个分隔符之间的grep子字符串 [英] grep substring between two delimiters

查看:133
本文介绍了两个分隔符之间的grep子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多bash脚本,它们使用grep中的perl表达式来提取两个定界符之间的子字符串.示例:

I have a lot of bash scripts that use perl expressions within grep in order to extract a substring between two delimiters. Example:

echo BeginMiddleEnd | grep -oP '(?<=Begin).*(?=End)'

问题是,当我将这些脚本移植到运行busybox的平台时,集成" grep无法识别-P开关.有没有一种干净的方法可以使用grepregular expressions来做到这一点?

The problem is, when I ported these scripts to a platform running busybox, 'integrated' grep does not recognize -P switch. Is there a clean way to do this using grep and regular expressions?

该平台上没有perlsedawk.这是一个轻量级的linux.

There is no perl, sed or awk on that platform. It's a lightweight linux.

推荐答案

假设每行最多出现一次,则可以使用

Assuming there's no more than one occurrence per line, you can use

sed -nr 's/.*Begin(.*)End.*/\1/p'

使用grep和非贪婪量词,您还可以每行打印多个.

With grep and non-greedy quantifier you could also print more than one per line.

这篇关于两个分隔符之间的grep子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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