Adding a new line after a specific line with sed


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Adding a new line after a specific line with sed
# 1  
Old 03-04-2016
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
Code:
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
Code:
sed 's!.*setenv.*!&/\nsetenv PRICESEQ "/invest1/iomaint/seqfiles/SHARED.CARDLIB"!' T3 > ${ORIG_JOB}_S

i am expecting the code to be

Code:
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 :
Code:
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.
# 2  
Old 03-04-2016
Hello gotamp,

Could you please try following and let me know if this helps you.
Code:
awk -vLINE="setenv PRICESEQ \"/invest1/iomaint/seqfiles/SHARED.CARDLIB\"" '/^setenv SEQFILES/{print $0 ORS LINE;next} 1'  Input_file

Output will be as follows.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 03-04-2016
Yes Ravinder. It works.

Problem stands resolved.
But just to satisfy my mind and soul if you could suggest with sed please !!
# 4  
Old 03-04-2016
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.
Code:
sed  '/setenv SEQFILES \"\/ConvWrk\/inteng03\/alltars\/bnymais1\"/ a setenv PRICESEQ \"/invest1/iomaint/seqfiles/SHARED.CARDLIB\""'   Input_file

Output will be as follows.
Code:
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
# 5  
Old 03-04-2016
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.
# 6  
Old 03-04-2016
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
Code:
sed '\!setenv! a\
setenv PRICESEQ "/invest1/iomaint/seqfiles/SHARED.CARDLIB"' T3 > ${ORIG_JOB}_S

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

2. Shell Programming and Scripting

sed adding a new line not working

The file which is used is /abc/apps/cobbbbbb/apps/abadv/binder/axyz.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/na6115.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/kc22.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/tr4823.bnd /abc/apps/cobbbbbb/apps/abcmp/binder/cpc0105.bnd The commads which I ran... (3 Replies)
Discussion started by: bhavanabahety
3 Replies

3. Shell Programming and Scripting

Adding text to the end of the specific line in a file(only to the first occurrence of it)

Hi, I want to add a text to the end of the specific line in a file. Now my file looks like this: 999 111 222 333 111 444 I want to add the string " 555" to the end of the first line contaning 111. Moreover, I want to insert a newline after this line containg the "000" string. The... (8 Replies)
Discussion started by: wenclu
8 Replies

4. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

5. Shell Programming and Scripting

Fill the empty line by adding line before blank line

FIle A "A" 2 aa 34 3 ac 5 cd "B" 3 hu 67 4 fg 5 gy output shud be A"" 2 aa 34 "A" 3 ac 34 "A" 5 cd 34 "B" 3 hu 67 "B" 4 fg 67 "B" 5 gy 67 (6 Replies)
Discussion started by: cdfd123
6 Replies

6. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

7. Shell Programming and Scripting

adding new line after finding specific text

hello i need some help here are the contents of my file. test.txt this is filename 1.mp3 http://www.url.com/filenamehashed filename 2.mp3 http://www.url.com/fileamehashed something_else.zip http://www.url.com/filenamehashed so this file has 100 of these lines filename url I would... (9 Replies)
Discussion started by: mscice
9 Replies

8. Shell Programming and Scripting

SED - adding a new line after pattern

Hi, In files, I have a field Date Of Birth (DOB). After that line I need to add Date of Joining (DOJ) DOB:19-Apr-1981 needs to become DOB:19-Apr-1981 DOJ:20-Jun-2005 What can be a sed/perl line that can do it for me. Please note that DOB/DOJ I have in variables I am doing in a... (6 Replies)
Discussion started by: eagercyber
6 Replies

9. Shell Programming and Scripting

Adding a columnfrom a specifit line number to a specific line number

Hi, I have a huge file & I want to add a specific text in column. But I want to add this text from a specific line number to a specific line number & another text in to another range of line numbers. To be more specific: lets say my file has 1000 lines & 4 Columns. I want to add text "Hello"... (2 Replies)
Discussion started by: Ezy
2 Replies

10. UNIX for Dummies Questions & Answers

sed - adding new line

I want to use sed to look for spaces in text and when find one move the next word to the next line. I used: sed 's/ /\n/g' out > new However when there is more than one space between two words it adds more lines between them. And I just want the words to be one under another. How can I... (2 Replies)
Discussion started by: sovixi
2 Replies
Login or Register to Ask a Question