Re: Sed or perl subsitutions - in place
Parvinder Bhasin <[EMAIL PROTECTED]> writes: > I am writing up a script to automatically increment the serial number > of bind dns zone file , but I am running across issues doing in place > substitution with either sed or even perl for that matter. I can do > this easily in Linux but am having hard time doing so in openbsd. I > would like to search for the serial number , increment by one and then > save the file. > > Any help...highly appreciated. > > Thx. > > Here is my code snippet: > > [...] Hello, Here is such a script I made: http://hyrule.folays.net/serial.pl <<< #!/usr/bin/perl -w use strict; use File::stat; $^I = ""; @ARGV = (); push @ARGV, glob "*.{net,org,fr,com,in-addr.arpa,local}"; push @ARGV, ("private", "public"); my @file = @ARGV; foreach (@file) { my $name = $_; my $sb = stat($name); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($sb->mtime); $year += 1900; $mon += 1; my $serial = sprintf("%d%02d%02d%02d", $year, $mon, $mday, 1); @ARGV = (); push @ARGV, $name; if ($sb->ctime == $sb->mtime) { while (<>) { if ($_ =~ m/;.*serial/i) { my $last = $_; $last =~ s/^([[:space:]]+)([[:digit:]]+)([[:space:]]*;.*serial.*)/$2/ix; chomp $last; ($serial = $last + 1) if (substr($last,0,8) eq substr($serial,0,8)); s/$last/$serial/; } print; } close STDIN; utime $sb->atime, $sb->mtime-1, $name; printf ";; SOA of $name\n"; printf "; serial: %s.\n", $serial; } else { printf ";; serial/modification time of $_ differ, skipping...\n"; } } >>> The basic idea is that it consider that if the inode time is the same that the modification time, then it will update the serial and set the modification time 1 second back of the inode time (thus removing the needs to keep a separate file to register when the file was last modified or even always updating serial of untouched files). The serial format is a string representation of the date + 2 serial digits I use it on occasionally manual changes, so the counter will overflow to the next day if you use it more than 100 times on a given file. -- folays
Re: Sed or perl subsitutions - in place
On Fri, Apr 04, 2008 at 01:07:10AM -0700, Parvinder Bhasin wrote: Hi! >*perl -p -i -e 's/$OLD/$NEW/' $file <--tried using perl > but still the file didn't change with the incremented serial number >sed 's/$OLD/$NEW/' $file <-I know this will only > search and replace but how do I do in in-place so that the file itself is > modified.* echo ",s/$OLD/$NEW/\nw" | ed -s $file -- rix http://www.ripe.net/perl/[EMAIL PROTECTED]
Re: Sed or perl subsitutions - in place
On 2008-04-04, Parvinder Bhasin <[EMAIL PROTECTED]> wrote: > OLD=`grep serial $file | awk '{print $1}'` > NEW=$(($OLD + 1)) > *perl -p -i -e 's/$OLD/$NEW/' $file hopefully the other posts should get you going, but beware! also search for the text 'serial' when you do the substitution, otherwise one day you will probably get bitten by subst'ing the wrong thing.
Re: Sed or perl subsitutions - in place
On Fri, Apr 04, 2008 at 01:07:10AM -0700, Parvinder Bhasin wrote: > I am writing up a script to automatically increment the serial number of > bind dns zone file , but I am running across issues doing in place > substitution with either sed or even perl for that matter. I can do > this easily in Linux but am having hard time doing so in openbsd. I > would like to search for the serial number , increment by one and then > save the file. > > Any help...highly appreciated. > > Thx. > > Here is my code snippet: > > #!/bin/sh > > for file in $(ls /var/named/master/*.file); > do > if [ -f $file ]; > then >OLD=`grep serial $file | awk '{print $1}'` >echo $OLD >NEW=$(($OLD + 1)) >echo $NEW >*perl -p -i -e 's/$OLD/$NEW/' $file <--tried using Works for me. Try just the perl -pi -e 's/foo/bar/' testfile.txt on its own first. You might have a problem with the substitution pattern. Check the timestamps of the target file before and after. It should work. What is the * doing before perl? > perl but still the file didn't change with the incremented serial number >sed 's/$OLD/$NEW/' $file <-I know this will only sed can not do it. perl can. > search and replace but how do I do in in-place so that the file itself > is modified.* > fi > done -- / Raimo Niskanen, Erlang/OTP, Ericsson AB
Re: Sed or perl subsitutions - in place
On Fri, Apr 4, 2008 at 10:07 AM, Parvinder Bhasin <[EMAIL PROTECTED]> wrote: > I am writing up a script to automatically increment the serial number of > bind dns zone file , but I am running across issues doing in place > substitution with either sed or even perl for that matter. I can do this > easily in Linux but am having hard time doing so in openbsd. I would like > to search for the serial number , increment by one and then save the file. > > Any help...highly appreciated. > > Thx. > > Here is my code snippet: > > #!/bin/sh > > for file in $(ls /var/named/master/*.file); > do > if [ -f $file ]; > then >OLD=`grep serial $file | awk '{print $1}'` >echo $OLD >NEW=$(($OLD + 1)) >echo $NEW >*perl -p -i -e 's/$OLD/$NEW/' $file <--tried using perl > but still the file didn't change with the incremented serial number >sed 's/$OLD/$NEW/' $file <-I know this will only > search and replace but how do I do in in-place so that the file itself is > modified.* > fi > done > > s/$OLD/$NEW/ should be in " not in ': perl -p -i -e "s/$OLD/$NEW/" "$file" -- error: one bad user found in front of screen
Re: Sed or perl subsitutions - in place
>sed 's/$OLD/$NEW/' $file <-I know this will only > search and replace but how do I do in in-place so that the file itself is > modified.* sed -a 's/old/new/wfilename' filename It is explained in: cd /usr/share/doc/usd/15.sed/; make paper.txt; less paper.txt Why dont you use the date as the serial?