Removing text between two specific strings
Solution 1
Replace everything from PI
to Value:
with empty string:
sed 's/PI.*Value://'
Solution 2
Using the d
command in sed
will delete the whole line. Also, I'm not sure why you're using <
and >
. Perhaps you're confusing them with \<
and \>
that grep
uses to denote word boundaries? In that case, you should know that sed
uses \b
for both types of word boundaries (beginning and ending). So you can write something like this:
sed -i 's/\bPI\b.*\bValue:\b//' your_file
For extra robustness, I would use perl
for lazy quantification of .
so that you only delete the text between the first occurrence of PI
and the first occurrence of Value:
. Of course it all depends on your use case.
perl -pi -e 's{ \b PI \b .*? \b Value: \b}{}x' your_file
Solution 3
Use this below code to remove multiple lines between patterns (including lines with patterns):
sed "/PI/,/Value:/d" your_file
If you want to modify your_file directly:
sed -i "/PI/,/Value:/d" your_file
Related videos on Youtube
khalil saad
Updated on September 18, 2022Comments
-
khalil saad over 1 year
I have a file as below:
mime PI Name: ISHO SUCCESS RATE RT, Value: 95.663826 scr PI Name: RRC Access Failures due to UU, Value: 0.13394141 prog PI Name: RRC Access Failures due to UU, Value: 0.16077702 sch PI Name: RRC Access Failures due to UU, Value: 0.11781933
I want to remove text
PI
tillValue:
. I triedsed '/<PI>/,/<\/Value:>/d'
Any help?
-
Brian Fitzpatrick over 8 yearsWill this work if
PI
andValue:
occur on different lines? -
Jeff Schaller over 4 yearsNotice the distinction here -- this deletes lines in a range, while the question is asking for deleting text between two strings.