Использование sed для удаления скобок из строки - PullRequest
3 голосов
/ 25 марта 2012

Я пытаюсь использовать 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]*, как подразумевает моя попытка работы.

Ответы [ 3 ]

4 голосов
/ 25 марта 2012

Это может работать для вас:

echo "Song Name (f/ featuredartist) (Remix)" | sed 's|(\(f/[^)]*\))|\1|'
Song Name f/ featuredartist (Remix)
1 голос
/ 25 марта 2012

раствор 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. (
1 голос
/ 25 марта 2012
echo 'Song Name (f/ featured artist) (Remix)' | sed 's/\(.*\)(\(f\/[^)]\+\))/\1\2/'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...