radya100
radya100

Reputation: 23

sed -> replace fixed text and parenthesis from string

How to bring this expression

echo "ObjectId(5e257e424ed10b0015e3e780),'qwe',ObjectId(5e257e424ed10b0015e3e780),()"

to this

5e257e424ed10b0015e3e780,'qwe',5e257e424ed10b0015e3e780,()

using sed?

I use this:

echo "ObjectId(5e257e424ed10b0015e3e780),'qwe',ObjectId(5e257e424ed10b0015e3e780),()" | \
   sed 's/ObjectId(\([a-z0-9]\)/\1/'

Upvotes: 2

Views: 1014

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

You may use

sed 's/ObjectId(\([[:alnum:]]*\))/\1/g'

See the online demo

The POSIX BRE pattern means:

  • ObjectId( - matches a literal string
  • \([[:alnum:]]*\) - Group 1: zero or more alphanumeric chars
  • ) - a literal ).

The \1 replacement will keep the Group 1 value only.

The g flag will replace all occurrences.

Upvotes: 7

Related Questions