2

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/'
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
radya100
  • 23
  • 2
  • You say expected is `5e257e424ed10b0015e3e780,'qwe',5e257e424ed10b0015e3e780,()`, but then you say "I cannot delete the final bracket". Please confirm what exact output you need. – Wiktor Stribiżew Feb 07 '20 at 09:31
  • Have you tried using a regex tester like https://regex101.com/ to see if your regular expression is correct? – Bart Feb 07 '20 at 09:33
  • @Bart regex101 should be used with caution when testing out patterns that should be POSIX BRE compliant, as the tool is only for PCRE, JS, Python `re` and Golang regex flavors. One should be aware how to "convert" the pattern later and what constructs to avoid. – Wiktor Stribiżew Feb 07 '20 at 09:35
  • @WiktorStribiżew Good point! radya100 Please match your particular regex flavour when using a test tool – Bart Feb 07 '20 at 09:36

1 Answers1

7

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563