Remove occurences of small words within a string

前端 未结 1 1224
慢半拍i
慢半拍i 2021-01-28 03:15

I\'m trying to remove a specific word from a string. I can\'t do a simple global string replace for \"the\" to empty string as \"the\" could be part of a word in the string.

1条回答
  •  情歌与酒
    2021-01-28 03:48

    Take a look at this sed:

    $ string='the_ad_an_feta_cfr_era_the_iop_the'
    $ sed -E -e ':a' -e 's/(^|_)(the|an|is|feta)(_|$)/\1/g;ta' -e 's/_$//' <<< "$string"
    ad_cfr_era_iop
    

    Note that the behavior of sed differs between Unix variants. Your sed seems to require newlines after labels (or multiple -e options). Further reading:

    • BSD/macOS Sed vs. GNU Sed vs. the POSIX Sed specification
    • Differences between sed on Mac OSX and other “standard” sed?

    Version without labels which is essentially the same as @Cyrus' answer but supports "items" with spaces:

    $ string='the_ad_an_feta_cfr_era_the cfr_the_iop_the'
    $ sed -E -e 's/_/__/g;s/(^|_)(the|an|is|feta)(_|$)//g;s/_+/_/g;s/^_//;s/_$//' <<< "$string"
    ad_cfr_era_the cfr_iop
    

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