sed rare-delimiter (other than & | / ?…)

后端 未结 8 2111
傲寒
傲寒 2020-12-17 17:42

I\'ve to apply the Unix command sed on a string (can contain #, !, /, ?, &, @ and all other characters) which can contains all types of

相关标签:
8条回答
  • 2020-12-17 18:31

    Escaping the delimiter inline for BASH to parse is cumbersome and difficult to read (although the delimiter does need escaping for sed's benefit when it's first used, per-expression).

    To pull together thkala's answer and user4401178's comment:

    DELIM=$(echo -en "\001");
    sed -n "\\${DELIM}${STARTING_SEARCH_TERM}${DELIM},\\${DELIM}${ENDING_SEARCH_TERM}${DELIM}p" "${FILE}"
    

    This example returns all results starting from ${STARTING_SEARCH_TERM} until ${ENDING_SEARCH_TERM} that don't match the SOH (start of heading) character with ASCII code 001.

    0 讨论(0)
  • 2020-12-17 18:32

    Wow. I totally did not know that you could use any character as a delimiter. At least half the time I use the sed and BREs its on paths, code snippets, junk characters, things like that. I end up with a bunch of horribly unreadable escapes which I'm not even sure won't die on some combination I didn't think of. But if you can exclude just some character class (or just one character even)

    echo '#01Y $#1+!' | sed -e 'sa$#1+ashita' -e 'su#01YuHolyug'

    > > > Holy shit! That's so much easier.

    0 讨论(0)
提交回复
热议问题