我正在尝试使用 sed (1)从字符串中删除括号,但仅当括号以特定字符串开头时才会删除。例如,我想将Song Name (f/ featured artist) (Remix)
之类的字符串更改为Song Name f/ featuredartist (Remix)
。我怎样才能做到这一点?
我目前正在尝试以下方法:
echo "Song Name (f/ featuredartist) (Remix)" | sed s/"(f\/ [a-z]*)"/"f\/ "/
但所有这一切都是返回Song Name f/ (Remix)
。
另请注意:任何事情都在f/
和)
之间,而不仅仅是[a-z]*
,因为我的工作尝试意味着。
答案 0 :(得分:4)
这可能对您有用:
echo "Song Name (f/ featuredartist) (Remix)" | sed 's|(\(f/[^)]*\))|\1|'
Song Name f/ featuredartist (Remix)
答案 1 :(得分:1)
echo 'Song Name (f/ featured artist) (Remix)' | sed 's/\(.*\)(\(f\/[^)]\+\))/\1\2/'
答案 2 :(得分:1)
TXR解决方案(http://www.nongnu.org/txr)。
@;; a texts is a collection of text pieces
@;; with no gaps in between.
@;;
@(define texts (out))@\
@(coll :gap 0)@(textpiece out)@(end)@\
@(cat out "")@\
@(end)
@;;
@;; recursion depth indicator
@;;
@(bind recur 0)
@;;
@;; a textpiece is a paren unit,
@;; or a sequence of chars other than parens.
@;; or, else, in the non-recursive case only,
@;; any character.
@;;
@(define textpiece (out))@\
@(cases)@\
@(paren out)@\
@(or)@\
@{out /[^()]+/}@\
@(or)@\
@(bind recur 0)@\
@{out /./}@\
@(end)@\
@(end)
@;;
@;; a paren unit consists
@;; of ( followed by a space-delimited token
@;; followed by some texts (in recursive mode)
@;; followed by a closing paren ).
@;; Based on what the word is, we transform
@;; the text.
@;;
@(define paren (out))@\
@(local word inner level)@\
@(bind level recur)@\
@(local recur)@\
@(bind recur @(+ level 1))@\
(@word @(texts inner))@\
@(cases)@\
@(bind recur 1)@\
@(bind word ("f/") ;; extend list here
)@\
@(bind out inner)@\
@(or)@\
@(bind out `(@word @inner)`)@\
@(end)@\
@(end)
@;; scan standard input in freeform (as one big line)
@(freeform)
@(texts out)@trailjunk
@(output)
@out@trailjunk
@(end)
示例运行:
$ txr paren.txr -
a b c d
[Ctrl-D]
a b c d
$ txr paren.txr -
The quick brown (f/ ox jumped over the (f/ lazy) dogs). (
The quick brown ox jumped over the (f/ lazy) dogs. (