grep substring between two delimiters
19,886
Solution 1
Assuming there's no more than one occurrence per line, you can use
sed -nr 's/.*Begin(.*)End.*/\1/p'
With grep and non-greedy quantifier you could also print more than one per line.
Solution 2
You can use awk
with custom field separator like this to get same output:
echo 'BeginMiddleEnd' | awk -F 'Begin|End' '{print $2}'
Middle
Solution 3
Use bash
built-in parameter substitution:
# grab some string from grep output
f=BeginMiddleEnd
middleend=${f/Begin/} # do some substitution to lose "Begin"
echo $middleend
MiddleEnd
beginmiddle=${f%%End} # strip from right end to lose "End"
echo $beginmiddle
BeginMiddle
Loads more examples here.
Author by
Ulrik
Updated on June 09, 2022Comments
-
Ulrik almost 2 years
I have a lot of
bash
scripts that useperl
expressions withingrep
in order to extract a substring between two delimiters. Example:echo BeginMiddleEnd | grep -oP '(?<=Begin).*(?=End)'
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 usinggrep
andregular expressions
?Edit: There is no
perl
,sed
orawk
on that platform. It's a lightweightlinux
. -
dokaspar almost 4 yearsA bit of an explanation wouldn't hurt ;). The -F option seems to be the field separator... but what magic is the
|
doing in-F 'Begin|End'
?