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

后端 未结 12 2528
春和景丽
春和景丽 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 06:00

    You can strip strings in Bash alone:

    $ foo="Here is a String"
    $ foo=${foo##*Here }
    $ echo "$foo"
    is a String
    $ foo=${foo%% String*}
    $ echo "$foo"
    is a
    $
    

    And if you have a GNU grep that includes PCRE, you can use a zero-width assertion:

    $ echo "Here is a String" | grep -Po '(?<=(Here )).*(?= String)'
    is a
    

提交回复
热议问题