Inserting text into a file but searching for the place to put it!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting text into a file but searching for the place to put it!
# 1  
Old 09-24-2010
Power Inserting text into a file but searching for the place to put it!

Hi

I would love a bit of help with a problem im having with a script. I need to insert a line of text which is saved in a variable called $fwInsert into a file whos name is saved in a variable called $server but it needs to be in a certain order.

The file is a forward file for a network and it will look something like this:
Code:
;zone file for test001.btcuk
$TTL 2d
@      IN     SOA    testserver.servers009.btcuk.    root.servers009.btcuk (
                        2010092401            ;               serial number with no of editings
                        24h                       ;               refresh
                        5m                        ;               update entry
                        1w                        ;               expirary
                        2h )                      ;                minimum
                        NS        testserver.servers009.btcuk
testmachine1      IN         A          192.189.1.1
testmachine2      IN         A          192.189.1.2
wibble                IN         A          192.189.1.42
tesmachineend    IN         A           192.189.1.255


I need to insert a machine into this file (i have the line of test like this in the variable $fwInsert as mentioned above) so I need to search through the file by ip address, find out where i'm supposed to put it and the paste it. I have tried grep but it error'd saying too many arguments and ive tried sed but it did nothing, no text was inserted but no error either which I suppose is an improvement.
Any help would be greatly appreciated.

Thanks

Katie

ps im using shell not korn Smilie

Last edited by Franklin52; 09-24-2010 at 09:57 AM.. Reason: Please use code tags!
# 2  
Old 09-24-2010
I assume the example is 'before insert', please show us after a successful update and a sample insert string.
# 3  
Old 09-24-2010
A successful insert would look like this:

Code:
;zone file for test001.btcuk
$TTL 2d
@      IN     SOA    testserver.servers009.btcuk.    root.servers009.btcuk (
                        2010092401            ;               serial number with no of editings
                        24h                       ;               refresh
                        5m                        ;               update entry
                        1w                        ;               expirary
                        2h )                      ;                minimum
                        NS        testserver.servers009.btcuk
testmachine1      IN         A          192.189.1.1
testmachine2      IN         A          192.189.1.2
testmachine3      IN         A          192.189.1.3
wibble                IN         A          192.189.1.42
tesmachineend    IN         A           192.189.1.255

# 4  
Old 09-24-2010
Code:
vi '+/192\.189\.1\.2' log_file << EOF
o$fwInsert^[:wq
EOF

where the ^[ is the ESC character, entered by typing CONTROL-V ESCAPE in vi insert mode.

Also. It may be tricky to add the backslashes between the IP numbers...
So, you could probably get away without using them... But the dot is a RE token.

maybe this might be a tad better:

Code:
vi +/" 192.189.1.2$" log_file << EOF
...

... or...

Code:
vi +/" $IP$" log_file << EOF
...


Last edited by quirkasaurus; 09-24-2010 at 12:20 PM.. Reason: mistake on IP address.
This User Gave Thanks to quirkasaurus For This Post:
# 5  
Old 09-24-2010
Code:
sed '/wibble/ i testmachine3      IN         A          192.189.1.3' file name

. This search for wibble line and add your line before that ( i flag is for insert). I m not sure is this what u want?
# 6  
Old 09-24-2010
Those are fab, thanks but my problem is that each file will have a different set of computers in it so i cant just search for the computer name. the file is sorted by ip address and thats what i need to search with. Any ideas?

I was thinking a while loop that starts from the ip address and takes one off each time and searches for it. If it doesnt find it at all then it inserts the file at the top?
# 7  
Old 09-24-2010
wow. at this point, how about a simplied input/output that shows all of your test cases?
better yet, you could post what you've tried so far and show what it's missing.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut text file in place

I have a file that i want to take only the first part of it and discard the rest, to be accurate,I need the first 137097 lines but I cant use split because I dont have enough space on my disck. I need sth to cut the file in its place (3 Replies)
Discussion started by: Heidi Heweidy
3 Replies

2. Shell Programming and Scripting

Help with sed and inserting text from another file

I need to insert text from one file into another file after specific term. I guess sed is the best method of doing this and I can insert a specified text string using this script but I am not sure how to modify it to insert text from another file: #!/bin/sh sed 's/\<VirtualHost... (17 Replies)
Discussion started by: barrydocks
17 Replies

3. Shell Programming and Scripting

Inserting text into a new file

Hi all, I want to create a file and then insert some text into it. I'm trying to create a .sh script that will create a new python file from a template. Can someone tell me why this won't work, touch $1 | sed -e '1i\Some test code here' Sorry I'm quite new to all this! Just as a side... (3 Replies)
Discussion started by: ahodgson
3 Replies

4. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

5. UNIX for Dummies Questions & Answers

Inserting a column into a text file

I have a tab delimited text file with multiple columns (data.txt). I would like to insert a column into the text file. The column I want to insert is in a text file (column.txt). I want to insert it into the 5th column of data.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

6. Shell Programming and Scripting

script for inserting line at specific place in file

I use zentyal for my server admin, which is great but zentyal auto-generates config file on boot and hence overwrites any changes made directly to config files. In order to allow multiple user access to a MS ACCESS database, I need to customise the smb.conf file and add the following line to the... (9 Replies)
Discussion started by: barrydocks
9 Replies

7. Shell Programming and Scripting

inserting a string to a text file

Hello Can somebody please help me with the following script? I'm trying to create a text file with 20 blank lines and then insert a string in line 2 but nothing is printed in the itxtfile. I can create the file with 20 blank lines but when I "tell" it to print something on the second line, it... (4 Replies)
Discussion started by: goude
4 Replies

8. Shell Programming and Scripting

Inserting text and modifying the file

I am in a dire need of doing this job , please help from shell script or perl script. It will be highly appreciated. Please have a look at the following INPUT file; The first 14 rows are not of interest but I want them to be included in the output file as they are. From the row 14... (3 Replies)
Discussion started by: digipak
3 Replies

9. Shell Programming and Scripting

Perl - Enter text in a file in a place.

Hi, I have a simple question: I need to enter some text in a text file at a certain place via perl. I would first need to find that specific text in the file and then I would like to insert a line after that particular line. Say I have this text file: I am a great Perl Programmer I... (1 Reply)
Discussion started by: som.nitk
1 Replies

10. UNIX for Dummies Questions & Answers

place to put statup scripts?

I have written a script to start websphere server, I dont know where to put the file in the OS . please put me the place to put the starup scripts in linux, solaris and AIX? (2 Replies)
Discussion started by: jayaramanit
2 Replies
Login or Register to Ask a Question