0

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'
2
  • So how does the sample output look like? hello tablename\ncopy tablename? Commented Apr 1, 2014 at 13:37
  • s/^(?=copy tablename\()/hello mytable\n/ should do the trick. Commented Apr 1, 2014 at 13:40

2 Answers 2

2

try this :

sed 's/^copy \([^(]*\)($/hello \1\ncopy \1(/' 
Sign up to request clarification or add additional context in comments.

1 Comment

+1 but you can simplify a bit: sed 's/^copy \([^(]*\)($/hello \1\n&/'
0

Using a perl one-liner

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.