Korn Shell script to insert at specific line

Hi,
I am trying to put together a Korn Shell script to insert at a specific line.
The system we use is SunOS 5.10

I can get the line number by using:-

num=`sed -n '/export ENV/=' ./tmp.file`

Not getting much headway using the above variable's value to insert -

export SYBASE=/opt/sybase15

after the above line number in tmp.file

Note - sed -i doesn't work on my unix box.

Thanks for your help.
AJ

Unless there's anything but exports in the script file there, order really doesn't matter -- you could append and it'd work.

But if you really want the line after export ENV:

awk -v N="new line to insert" 'N && /export ENV/ { $0=$0"\n"N ; N="" } 1' < inputfile > outputfile

Thanks.
I tried -

awk -v N="export SYBASE=/opt/sybase15" 'N && /export ENV/ { $0=$0"\n"N ; N="" } 1' < ./tmp.file > ./tmp.fileZ

I get the following error though-
awk: syntax error near line 1
awk: bailing out near line 1

Also is there any way to do an edit in-place instead of writing to a different file?

---------- Post updated at 03:49 PM ---------- Previous update was at 03:36 PM ----------

SOLVED:-
I was able to do an in-place insert with the following;-

perl -i.bak -lpe 's/export ENV/export ENV\nexport SYBASE/g' ./tmp.file

On Solaris please use nawk not awk instead.

1 Like

nawk seems to have done the trick.
Anyway nawk can be used to edit in - place rather than copy to another file?

I never recommend editing in-place. One mistake and you'll trash all your originals.

1 Like