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 till Value:. I tried

sed '/<PI>/,/<\/Value:>/d' 

Any help?


Replace everything from PI to Value: with empty string:

sed 's/PI.*Value://'
  • 11
    Will this work if PI and Value: occur on different lines? Aug 24 '15 at 11:39

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

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
  • Notice the distinction here -- this deletes lines in a range, while the question is asking for deleting text between two strings.
    – Jeff Schaller
    Jul 31 '19 at 14:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.