I am trying to output a string that contains everything between two words of a string:
input:
"Here is a String"
output:
"is a"
Using:
sed -n '/Here/,/String/p'
includes the endpoints, but I don't want to include them.
I am trying to output a string that contains everything between two words of a string:
input:
"Here is a String"
output:
"is a"
Using:
sed -n '/Here/,/String/p'
includes the endpoints, but I don't want to include them.
Through GNU awk,
grep with
-P
(perl-regexp) parameter supports\K
, which helps in discarding the previously matched characters. In our case , the previously matched string wasHere
so it got discarded from the final output.If you want the output to be
is a
then you could try the below,To understand
sed
command, we have to build it step by step.Here is your original text
Let's try to remove
Here
withs
ubstition option insed
At this point, I believe you would be able to remove
String
as wellBut this is not your desired output.
To combine two sed commands, use
-e
optionHope this helps
Simple grep can also support positive & negative look-ahead & look-back: For your case, the command would be:
If you have a long file with many multi-line ocurrences, it is useful to first print number lines:
This might work for you (GNU sed):
This presents each representation of text between two markers (in this instance
Here
andString
) on a newline and preserves newlines within the text.