Adding a new line after a specific line with sed

Hi All,

My requirement is to add a specific line in a file after a certain line that contains 'setenv'

the existing code is like

setenv SEQFILES "/ConvWrk/inteng03/alltars/bnymais1"

LIBDEF scope='JOB' type='PGM' dataset='SUNAR.PJ90000P.JOBLIB' lib='/invest1/io09100/bnymport:/invest1/io09100/mbm:/bnymasia/invest1:'

I am using

sed 's!.*setenv.*!&/\nsetenv PRICESEQ "/invest1/iomaint/seqfiles/SHARED.CARDLIB"!' T3 > ${ORIG_JOB}_S

i am expecting the code to be

setenv SEQFILES "/ConvWrk/inteng03/alltars/bnymais1"
setenv PRICESEQ "/invest1/iomaint/seqfiles/SHARED.CARDLIB"

LIBDEF scope='JOB' type='PGM' dataset='SUNAR.PJ90000P.JOBLIB' lib='/invest1/io09100/bnymport:/invest1/io09100/mbm:/bnymasia/invest1:'

however its not taking the next line
its getting appended in the same line itself.

my output :


setenv SEQFILES "/ConvWrk/inteng03/alltars/bnymus1"/nsetenv PRICESEQ "/invest1/iomaint/seqfiles/SHARED.CARDLIB"

LIBDEF scope='JOB' type='PGM' dataset='SUNAR.PJ90000P.JOBLIB' lib='/invest1/io09100/bnymport:/invest1/io09100/mbm:/bnymasia/invest1:'

i know i am commiting a silly mistake.. Kindly rectify.

Hello gotamp,

Could you please try following and let me know if this helps you.

awk -vLINE="setenv PRICESEQ \"/invest1/iomaint/seqfiles/SHARED.CARDLIB\"" '/^setenv SEQFILES/{print $0 ORS LINE;next} 1'  Input_file

Output will be as follows.

setenv SEQFILES "/ConvWrk/inteng03/alltars/bnymais1"
setenv PRICESEQ "/invest1/iomaint/seqfiles/SHARED.CARDLIB"
 
LIBDEF scope='JOB' type='PGM' dataset='SUNAR.PJ90000P.JOBLIB' lib='/invest1/io09100/bnymport:/invest1/io09100/mbm:/bnymasia/invest1:'

Thanks,
R. Singh

1 Like

Yes Ravinder. It works.

Problem stands resolved.
But just to satisfy my mind and soul if you could suggest with sed please !!

Hello gotamp,

Yes, mind and soul's satisfaction is very much important. So here is a sed solution which may help you. Also I tested this in GNU sed only.

sed  '/setenv SEQFILES \"\/ConvWrk\/inteng03\/alltars\/bnymais1\"/ a setenv PRICESEQ \"/invest1/iomaint/seqfiles/SHARED.CARDLIB\""'   Input_file

Output will be as follows.

setenv SEQFILES "/ConvWrk/inteng03/alltars/bnymais1"
setenv PRICESEQ "/invest1/iomaint/seqfiles/SHARED.CARDLIB""
  
LIBDEF scope='JOB' type='PGM' dataset='SUNAR.PJ90000P.JOBLIB' lib='/invest1/io09100/bnymport:/invest1/io09100/mbm:/bnymasia/invest1:'

Thanks,
R. Singh

The sed in post#1 works for me. Might be a sed version incompatibiity, or, you are on a system that doesn't have \n as the line terminator.

Unix sed needs a backslash-newline in the sed code. It can be done with the s command, but the a command is more elegant

sed '\!setenv! a\
setenv PRICESEQ "/invest1/iomaint/seqfiles/SHARED.CARDLIB"' T3 > ${ORIG_JOB}_S