两个分隔符之间的grep子字符串 [英] grep substring between two delimiters
问题描述
我有很多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开关.有没有一种干净的方法可以使用grep
和regular 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
?
该平台上没有perl
,sed
或awk
.这是一个轻量级的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屋!