Korn Shell script to insert at specific line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Korn Shell script to insert at specific line
# 1  
Old 04-17-2012
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:-
Code:
num=`sed -n '/export ENV/=' ./tmp.file`


Not getting much headway using the above variable's value to insert -
Code:
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

Last edited by aj8200; 04-17-2012 at 04:28 PM..
# 2  
Old 04-17-2012
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:

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

# 3  
Old 04-17-2012
Thanks.
I tried -
Code:
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
# 4  
Old 04-17-2012
On Solaris please use nawk not awk instead.
This User Gave Thanks to methyl For This Post:
# 5  
Old 04-17-2012
nawk seems to have done the trick.
Anyway nawk can be used to edit in - place rather than copy to another file?
# 6  
Old 04-18-2012
I never recommend editing in-place. One mistake and you'll trash all your originals.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert new line after a specific character in scripts?

Hi, I'm trying to add a new line after finding a specific String. That is my string: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html and I want to change that file to: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html extra_hosts: -... (4 Replies)
Discussion started by: siamak
4 Replies

2. Solaris

Insert a file at specific line

Hi, Anyone can help me in Solaris command on how to insert a file at specific line. I want file1.sql content to be inserted on file2.sh after "recover database using backup controlfile until cancel". # file1.sql /archivelogs/927_822338133.arc /archivelogs/671_822338107.arc... (3 Replies)
Discussion started by: fspalero
3 Replies

3. Shell Programming and Scripting

Insert text line to specific location CSV

In Perl. ***edited question below*** Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file: age,name,locationIs it possible to INSERT data into the CSV into the correct age order. For example, if I had... (1 Reply)
Discussion started by: whyte_rhyno
1 Replies

4. UNIX for Dummies Questions & Answers

shell script : log to txt and insert new line everytime

Hi, I have this script, while do ps ax|grep 5060 > log.txt echo " " sleep 1 done } I want to actually put a new line everytime the loop is executed in log.txt , but I do not know how to "embed" the echo " " inside the log.txt. (so to say... (1 Reply)
Discussion started by: peuceul
1 Replies

5. Shell Programming and Scripting

Insert specific line when found similar value

Hi All, I have file like this: file1: 3778 10474 24 3778 10475 24 3778 10476 25 3778 10495 26 3794 10001 33 3794 10002 33 3794 10004 33 3794 10007 34 3794 10008 34 3794 10011 34 3794 10012 34 3794 10013 34 3794 10017 34 3810 10282 27 (6 Replies)
Discussion started by: attila
6 Replies

6. Shell Programming and Scripting

How to read from specific line number in shell script?

I have a text file which is having 30000 lines in it. I have to create a xml file for each 10000 lines until all the lines in the text files are written. Also please help how can i get number of lines in the text file in a shell variable? (19 Replies)
Discussion started by: vel4ever
19 Replies

7. Shell Programming and Scripting

Print pipe separated list as line by line in Korn Shell

Korn Shell in AIX 6.1 I want to print the below shown pipe (|) separated list line by line. line=es349889|nhb882309|ts00293|snh03524|bg578835|bg37900|rnh00297|py882201|sg175883 for i in line do echo "Hello $line " done I wanted to execute the above for loop. But i can't even set the... (3 Replies)
Discussion started by: polavan
3 Replies

8. Shell Programming and Scripting

Korn shell to insert cyrillic characters into the databse

i have written a shell script that reads a csv file and inserts tokenized strings into the database. the problem comes when the csv file has cyrillic characters. how do i set the parameters in my shell script(korn shell) so that any characters can be inserted into the database. (3 Replies)
Discussion started by: vkca
3 Replies

9. Shell Programming and Scripting

Korn shell program to parse CSV text file and insert values into Oracle database

Enclosed is comma separated text file. I need to write a korn shell program that will parse the text file and insert the values into Oracle database. I need to write the korn shell program on Red Hat Enterprise Linux server. Oracle database is 10g. (15 Replies)
Discussion started by: shellguy
15 Replies

10. Shell Programming and Scripting

Shell script to parse a line and insert a word

Hi All, I have a file like this, data1,data2,,,data5,data6. i want to write a shell script to replace data3 with "/example/string". which means my data file should look like this . data1,data2,example/string],,data5,data6. Could you guys help me to get a sed command or any other command... (8 Replies)
Discussion started by: girish.raos
8 Replies
Login or Register to Ask a Question