John Humphreys
John Humphreys

Reputation: 39354

Regex replace (sed or perl) with variable capture

I need to do a regex capture/repalce on a file with content like the following. While I'm okay at matching things, I'm having trouble doing a capture replace.

I'd like to do the following:

  1. Capture which will be different in every instance.
  2. Add a new line before the copy line that references the table name.

So, for example, copy mytable( would become hello mytable\ncopy mytable(.


Sample Input

copy tablename(
        preferredid= 'c0»',
        qid= 'c0»' with null(''),
        qpi= 'c0»',
        ptid= 'c0»' with null(''))...

into '/idata2/backup/core/eq.ingres'

Upvotes: 0

Views: 253

Answers (2)

Miller
Miller

Reputation: 35208

Using a perl one-liner

perl -i -pe 's/^(?=copy (\w+)\()/hello $1\n/' file

Upvotes: 0

Farvardin
Farvardin

Reputation: 5424

try this :

sed 's/^copy \([^(]*\)($/hello \1\ncopy \1(/' 

Upvotes: 2

Related Questions