Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to insert lines in a file 3

Status
Not open for further replies.

netman4u

Technical User
Mar 16, 2005
176
US
Hello all,

I have many text files I need to add a line at a certain point. A sample of the file is below:

Code:
SYNTAX_VERSION 7


LOGFILE "ADSM/AIX logs"
        DESCRIPTION "ADSM backup message log on AIX"
        LOGPATH "/usr/lpp/adsm/bin/dsmsched.log"
        INTERVAL "15m"
        CHSET ISO88591
        FROM_LAST_POS
        CLOSE_AFTER_READ
        SEVERITY Unknown
        APPLICATION "ADSM"
        MSGGRP "Unix_Support"
        MSGCONDITIONS
                DESCRIPTION "password expired"
                CONDITION_ID "aad654f8-ec52-71d7-1fca-aa0615200000"
                CONDITION
                        TEXT "Your password has expired."
                SET
                        SEVERITY Critical
                        TEXT "The ADSM client password has expired!"
                        HELPTEXT "The password for the client portion of the ADSM
Backup Software has expired.  This system will not
be able to backup to the mainframe until this is
corrected.

Contact the HPUX oncall person during the day or
leave a phonemail for them."
                        HELP "6fa76d9da5b2.02.80.01.11.2c.00.00.00"
                DESCRIPTION "info: number of bytes transferred"
                CONDITION_ID "aaeee202-ec52-71d7-1fca-aa0615200000"
                CONDITION
                        TEXT "<@.date> <@.time> Total number of bytes transferred: <*.bytes>"
                SET
                        SEVERITY Normal
                        TEXT "Successful backup: <bytes> bytes on <date> at <time>"
                DESCRIPTION "info: data transfer rate"
                CONDITION_ID "aaf50bf0-ec52-71d7-1fca-aa0615200000"
                CONDITION
                        TEXT "Data transfer rate: <*.rate>"
                SET
                        SEVERITY Normal
                        TEXT "Successful backup: data transfer rate <rate>"
                DESCRIPTION "info: compression"
                CONDITION_ID "aaf93d24-ec52-71d7-1fca-aa0615200000"
                CONDITION
                        TEXT "Compression percent reduction: <*.compress>"
                SET
                        SEVERITY Normal
                        TEXT "Successful backup: <compress> compression reduction"

What I need to do is insert a line with "NOTIFICATION" in it just below each "SET" as in:

Code:
SYNTAX_VERSION 7


LOGFILE "ADSM/AIX logs"
        DESCRIPTION "ADSM backup message log on AIX"
        LOGPATH "/usr/lpp/adsm/bin/dsmsched.log"
        INTERVAL "15m"
        CHSET ISO88591
        FROM_LAST_POS
        CLOSE_AFTER_READ
        SEVERITY Unknown
        APPLICATION "ADSM"
        MSGGRP "Unix_Support"
        MSGCONDITIONS
                DESCRIPTION "password expired"
                CONDITION_ID "aad654f8-ec52-71d7-1fca-aa0615200000"
                CONDITION
                        TEXT "Your password has expired."
                SET
                        [b]NOTIFICATION[/b]                      
                        SEVERITY Critical
                        TEXT "The ADSM client password has expired!"
                        HELPTEXT "The password for the client portion of the ADSM
Backup Software has expired.  This system will not
be able to backup to the mainframe until this is
corrected.

Contact the HPUX oncall person during the day or
leave a phonemail for them."
                        HELP "6fa76d9da5b2.02.80.01.11.2c.00.00.00"
                DESCRIPTION "info: number of bytes transferred"
                CONDITION_ID "aaeee202-ec52-71d7-1fca-aa0615200000"
                CONDITION
                        TEXT "<@.date> <@.time> Total number of bytes transferred: <*.bytes>"
                SET
                        [b]NOTIFICATION[/b]
                        SEVERITY Normal
                        TEXT "Successful backup: <bytes> bytes on <date> at <time>"
                DESCRIPTION "info: data transfer rate"
                CONDITION_ID "aaf50bf0-ec52-71d7-1fca-aa0615200000"
                CONDITION
                        TEXT "Data transfer rate: <*.rate>"
                SET
                        [b]NOTIFICATION[/b]
                        SEVERITY Normal
                        TEXT "Successful backup: data transfer rate <rate>"
                DESCRIPTION "info: compression"
                CONDITION_ID "aaf93d24-ec52-71d7-1fca-aa0615200000"
                CONDITION
                        TEXT "Compression percent reduction: <*.compress>"
                SET
                        [b]NOTIFICATION[/b]
                        SEVERITY Normal
                        TEXT "Successful backup: <compress> compression reduction"





If at first you don't succeed, don't try skydiving.
 
Try,
Code:
my $file = "/path/to/file" ;
fopen( FH, "$file" ) ;
my $content = <FH> ;
fclose( FH ) ;
$content = s/SET/SET\nNOTIFICATION/g ;
fopen( FH, ">$file" ) ;
fwrite( FH, $content ) ;
fclose( FH ) ;
P.S. Not Tested

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
That code should work in most cases but it's a bit dangerous. If you have SET anywhere else in the text it will also be replaced with the NOTIFICATION text.

This code reads the file line-by-line and directs it to a new file. If you want to write to the same file, use spookie's code with the regular expression from my code to make sure SET is on its own line.

Code:
use strict;

[COLOR=grey]# The file you want to load[/color]
my $f = "filename.log";

[COLOR=grey]# Open the old file and a new file (new file has .new extension)[/color]
open (READ, "$f") || die("Cannot open $f: $!");
open (WRITE, ">$f.new") || die("Cannot write to $f.new: $!");

[COLOR=grey]# Slurp in the log file[/color]
while (<READ>) {
  [COLOR=grey]# Write the line to the output file[/color]
  print WRITE;

  [COLOR=grey]# If this was a SET line (with nothing else but spaces on the line)[/color]
  [COLOR=grey]# write NOTIFICATION on the next line.[/color]
  if (/^\s*SET\s*$/i) {
    print WRITE "                        NOTIFICATION\n"
  }
}

close (READ);
close (WRITE);
 
Thanks for the replies but not quite working. Here is my code:

Code:
use strict;
use warnings;
my @paths = `find /var/opt/opv_data -name "*.dat"`;
chomp @paths;
foreach  (@paths) {
	fopen( FH, "$_" ) ;
	my $content = <FH> ;
	fclose( FH ) ;
	$content =  s/SET/SET\nNOTIFICATION/g ;  #(/^\s*SET\s*$/i) 
	fopen( FH, ">$_" ) ;
	fwrite( FH, $content ) ;
	fclose( FH ) ;
}

Here is the output:

Code:
# ./alrmpnt_util_2.pl        
Bareword "FH" not allowed while "strict subs" in use at ./alrmpnt_util_2.pl line 12.
Bareword "FH" not allowed while "strict subs" in use at ./alrmpnt_util_2.pl line 14.
Bareword "FH" not allowed while "strict subs" in use at ./alrmpnt_util_2.pl line 16.
Bareword "FH" not allowed while "strict subs" in use at ./alrmpnt_util_2.pl line 17.
Bareword "FH" not allowed while "strict subs" in use at ./alrmpnt_util_2.pl line 18.
Execution of ./alrmpnt_util_2.pl aborted due to compilation errors.
{/code]

And when I comment out "use strict":

[code]
# ./alrmpnt_util_2.pl
Undefined subroutine &main::fopen called at ./alrmpnt_util_2.pl line 12.
# 
{/code]

If at first you don't succeed, don't try skydiving.
 
[tt]fopen[/tt], [tt]fwrite[/tt], and [tt]fclose[/tt] are all C functions. There may be a way to enable them in perl, and in fact Perl just maps its own functions back to the C calls. However, they need to be changed to the appropriate perl functions.

If you change
[tt]fopen( FH, "$_" )[/tt] to [tt]open( FH, "$_" )[/tt]
[tt]fwrite( FH, $content )[/tt] to [tt]print FH $content[/tt]
[tt]fclose( FH )[/tt] to [tt]close ( FH )[/tt]
your code should work.
 
Hmmmm, now it just seems to erase the whole file!

If at first you don't succeed, don't try skydiving.
 
[tt]$content =~ s/SET/SET\nNOTIFICATION/g ;[/tt] Note the previously missing ~ in =~
 
Thanks colorplane and spookie. I have it working with a few mods on color's code:

Code:
use strict;
use warnings;
my @paths = `find /var/opt/opv_data -name "*.dat"`;
chomp @paths;
foreach  my $path (@paths) {
	open (READ, "$path") || die("Cannot open $_: $!");
	open (WRITE, ">$path.new") || die("Cannot write to $_.new: $!");
	while (<READ>) {
		print WRITE;
		if (/^\s*SET\s*$/i) {
			print WRITE "                        NOTIFICATION\n"
		}
	}
	close (READ);
	close (WRITE);
	unlink $path;
	my $results = `mv "$path.new" $path`;
	print "$results\n";
}

If at first you don't succeed, don't try skydiving.
 
pure perl solution:

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[blue]@ARGV[/blue] = [url=http://perldoc.perl.org/functions/glob.html][black][b]glob[/b][/black][/url][red]([/red][red]'[/red][purple]/var/opt/opv_data/*.dat[/purple][red]'[/red][red])[/red][red];[/red]
[gray][i]#print "$_\n" for @ARGV;[/i][/gray]
[blue]$^I[/blue] = [red]'[/red][purple].bak[/purple][red]'[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<>[red])[/red] [red]{[/red]
   [olive][b]if[/b][/olive] [red]([/red][red]/[/red][purple]^([purple][b]\s[/b][/purple]*SET[purple][b]\s[/b][/purple]*)$[/purple][red]/[/red][red])[/red] [red]{[/red]
       [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple][blue]$1[/blue]                        NOTIFICATION[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
   [red]}[/red]
   [olive][b]else[/b][/olive] [red]{[/red]
       [black][b]print[/b][/black][red];[/red]
   [red]}[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.8.8) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

You can unlink the backup files easy enough if need be:

unlink glob('/var/opt/opv_data/*.dat.bak');

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top