Using sed to strip parentheses from a string

I'm trying to use sed (1) to remove parentheses from lines, but only when the parentheses start on a specific line. For example, I want to change a line such as Song Name (f/ featured artist) (Remix)

to Song Name f/ featuredartist (Remix)

. How can I achieve this?

I am currently trying to do the following:

echo "Song Name (f/ featuredartist) (Remix)" | sed s/"(f\/ [a-z]*)"/"f\/ "/


But it all returns Song Name f/ (Remix)


Also note that anything goes between f/

and )

, not just [a-z]*

as my working endeavor implied.


source to share

3 answers

This might work for you:

echo "Song Name (f/ featuredartist) (Remix)" | sed 's|(\(f/[^)]*\))|\1|'
Song Name f/ featuredartist (Remix)




echo 'Song Name (f/ featured artist) (Remix)' | sed 's/\(.*\)(\(f\/[^)]\+\))/\1\2/'




TXR solution ( ).

@;; a texts is a collection of text pieces
@;; with no gaps in between.
@(define texts (out))@\
  @(coll :gap 0)@(textpiece out)@(end)@\
  @(cat out "")@\
@;; 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))@\
     @(paren out)@\
     @{out /[^()]+/}@\
     @(bind recur 0)@\
     @{out /./}@\
@;; 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))@\
    @(bind recur 1)@\
    @(bind word ("f/") ;; extend list here
    @(bind out inner)@\
    @(bind out `(@word @inner)`)@\
@;; scan standard input in freeform (as one big line)
@(texts out)@trailjunk


Run example:

$ txr paren.txr -
a b c 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. (




All Articles