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!
# 8  
Old 09-24-2010
Quote:
Originally Posted by KatieV
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?
I've cooked up a quick Perl script for this problem. Given a line that has an IP address, the script loops through the file, compares the IP addresses and plugs the input line wherever appropriate.

Code:
$
$
$
$ # show the content of the data file "f21"
$ cat -n f21
     1  ;zone file for test001.btcuk
     2  $TTL 2d
     3  @      IN     SOA    testserver.servers009.btcuk.    root.servers009.btcuk (
     4                          2010092401            ;               serial number with no of editings
     5                          24h                       ;               refresh
     6                          5m                        ;               update entry
     7                          1w                        ;               expirary
     8                          2h )                      ;                minimum
     9                          NS        testserver.servers009.btcuk
    10  testmachine1      IN         A          192.189.1.1
    11  testmachine2      IN         A          192.189.1.2
    12  wibble            IN         A          192.189.1.42
    13  tesmachineend     IN         A          192.189.1.255
$
$
$ # show the content of the Perl script "insertline.pl"
$ # hopefully the script comments are descriptive enough
$ cat -n insertline.pl
     1  #!perl -w
     2  # Name:  insertline.pl
     3  # Desc:  Inserts a line in the data file depending upon the
     4  #        comparison of IP addresses. I've assumed that lines containing IP
     5  #        addresses are at the end of the file.
     6  # Usage: perl insertline.pl "$server" "$fwInsert"
     7  $file = $ARGV[0];                                 # read in the file name
     8  $input = $ARGV[1];                                # read in the input line
     9  $inserted = 0;                                    # assume it's not inserted yet
    10  @x = split/[ .]+/,$input;                         # split input line to capture IP in $x[3,4,5,6]
    11  open (F, $file) or die "Can't open $file: $!";    # open file
    12  while (<F>) {                                     # loop through it
    13    chomp($line = $_);                              # chomp newline
    14    $ln = $line;                                    # assign current line to temp variable
    15    $ln =~ s/^\s+//g; $ln =~ s/\s+$//g;             # trim spaces from temp variable
    16    @fields = split(/[ \t]+/, $ln);                 # split its fields and store in array @fields
    17    if ($#fields == 3 and                           # if count of fields is 4 and
    18        $fields[3] =~ /^\d+(.\d+){3}/ and           # if the last field is an IP address and
    19        not $inserted) {                            # if input line hasn't been inserted yet
    20      @y = split/\./,$fields[3];                    # then capture IP address of current line in @y
    21      if (($y[0] <=> $x[3] ||                       # compare IPs by logical disjunction
    22           $y[1] <=> $x[4] ||
    23           $y[2] <=> $x[5] ||
    24           $y[3] <=> $x[6]) > 0)                    # if file IP > input IP
    25      {
    26        print $input,"\n",$line,"\n";               # then print input IP followed by file IP
    27        $inserted = 1;                              # and set inserted flag to true (so no more inserts)
    28      } else {
    29        print $line,"\n";                           # otherwise just print the line from file that has IP
    30      }
    31    } else {
    32      print $line,"\n";                             # if no IP in current line, then just print it
    33    }
    34  }
    35  print $input,"\n" if not $inserted;               # if input IP hasn't been added yet then do it now
    36  close (F) or die "Can't close $file: $!";         # clean up after ourselves. Ciao!
$
$
$ # define the shell variables
$ server="f21"
$ fwInsert="testmachine3      IN         A          192.189.1.3"
$
$
$ # invoke the script passing the shell variables to it
$ perl insertline.pl "$server" "$fwInsert"
;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
$
$
$ # try the start boundary condition
$ fwInsert="testmachine3      IN         A          192.177.1.128"
$ perl insertline.pl "$server" "$fwInsert"
;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
testmachine3      IN         A          192.177.1.128
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
$
$
$ # try the end boundary condition
$ fwInsert="testmachine3      IN         A          192.199.1.0"
$ perl insertline.pl "$server" "$fwInsert"
;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
testmachine3      IN         A          192.199.1.0
$
$
$

HTH,
tyler_durden

Last edited by durden_tyler; 09-24-2010 at 03:49 PM..
This User Gave Thanks to durden_tyler For This Post:
# 9  
Old 09-24-2010
Where's that perl script? I would like to learn from your work!
# 10  
Old 09-24-2010
Quote:
Originally Posted by adelsin
Where's that perl script? ...
Umm... where's that perl script ??
It's right there in my post - immediately below "cat -n insertline.pl" starting from line 1 and ending at line 36 !

tyler_durden
# 11  
Old 09-24-2010
lulz. Sorry.
# 12  
Old 09-24-2010
Code:
#!/usr/bin/env ruby 

require 'ipaddr'
def dec2ip(dec) return IPAddr.new(dec, Socket::AF_INET).to_s end
def ip2dec(ip) return IPAddr.new(ip).to_i end
file=ARGV[0]
insertline=ARGV[1]
data=File.read(file) # read the whole file
# store all ip address
sortedip=data.scan(/(\d+\.\d+\.\d+\.\d+)/).flatten.map {|x| ip2dec(x)}
# get the ip address to be inserted
insertip=ip2dec(insertline.scan(/\d+\.\d+\.\d+\.\d+/)[0])
sortedip<<insertip
sortedip.sort!
# get the ip before the one being inserted
beforeip=dec2ip(sortedip[sortedip.index(insertip)-1])
data.split("\n").each do |x|
  x="#{x}\n#{insertline}\n"  if x.match(beforeip)
  puts x
end


Code:
$ echo $insert
testmachine4 IN A 192.189.1.4

# ruby test.rb file "$insert"
;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
testmachine4      IN         A          192.189.1.4
wibble                IN         A          192.189.1.42
tesmachineend    IN         A           192.189.1.255


$ insert="testmachine20      IN         A          192.190.1.5"
$ ruby test.rb file "$insert"
;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
testmachine20      IN         A          192.190.1.5

This User Gave Thanks to kurumi For This Post:
# 13  
Old 10-07-2010
Hi

Thanks for this code, its brill. I would love to use durden_tyler s perl (always wanted to learn perl, not touched it before though)

One problem is that the IP address isnt always at the end of the line. Soemtimes there is a description at the end of the line like this:

Code:
testmachinedesc       IN        A      192.189.1.42 ; used for stuff

Also, i just tested this on the dev virtual machine I have because the test files dont have descriptions (hence the confusion, sorry!) and it just prints the input a couple of lines after the bottom of the file (or at least it looks like it does when you print out the file when you run the perl) but when you go into the file its not there O_O.

Thanks for your help!

Last edited by KatieV; 10-07-2010 at 12:39 PM.. Reason: Tested script
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