How to use sed/grep to extract text between two words?

后端 未结 12 2529
春和景丽
春和景丽 2020-11-22 05:25

I am trying to output a string that contains everything between two words of a string:

input:

\"Here is a String\"

output:

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 05:48

    To understand sed command, we have to build it step by step.

    Here is your original text

    user@linux:~$ echo "Here is a String"
    Here is a String
    user@linux:~$ 
    

    Let's try to remove Here string with substition option in sed

    user@linux:~$ echo "Here is a String" | sed 's/Here //'
    is a String
    user@linux:~$ 
    

    At this point, I believe you would be able to remove String as well

    user@linux:~$ echo "Here is a String" | sed 's/String//'
    Here is a
    user@linux:~$ 
    

    But this is not your desired output.

    To combine two sed commands, use -e option

    user@linux:~$ echo "Here is a String" | sed -e 's/Here //' -e 's/String//'
    is a
    user@linux:~$ 
    

    Hope this helps

提交回复
热议问题