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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Line replace/Modify script

Status
Not open for further replies.
Jun 3, 2007
84
US
I am trying to write a script to edit a file when matched it will replace the line with the new static entry. I am a bit stuck and wondering if someone could please help me!!

For example I want an entire line to be removed if a match is found anywhere on that line using a regex of .*[0-9]{4}.*
test line with no number
this is a test line
this is a test line 5677
this is a test line 2334
this is a test line 1235 this is a test line

The problem with my code is that the lines that don't match also get removed. What I am trying to do is have the script add the lines that don't match into the new file without modifying them as well up appending the lines that match. Right now the script is only adding/appending the lines that match the regex and not doing anything with the ones that don't. Output shown below

this worked
this worked
this worked

Any ideas help? I would really appreciate it.




open(FILE,$ARGV[0]);
@array1 = <FILE>;

open(NEWFILE,">/home/testuser01/testfile.txt");
foreach $line(@array1)
{
chomp $line;

if($line =~ /id\:745[0-9]{4}/)
{
$line =~ s/.*id\:745[0-9]{4}.*/this worked/sgi;
print NEWFILE $line, "\n";
}

else
{
print NEWFILE "$_" , "\n";
print $_;
}
}
close($FILE);
close($NEWFILE);
 
in the "else" block, why are you using "$_" instead of $line?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Kevin has pointed out the exact bug in your code. Here is the rest of your code cleaned up some though:

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]

[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$infile[/blue] = [blue]$ARGV[/blue][red][[/red][fuchsia]0[/fuchsia][red]][/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Specify an input file[/purple][red]"[/red][red];[/red]

[black][b]my[/b][/black] [blue]$outfile[/blue] = [red]'[/red][purple]/home/testuser01/testfile.txt[/purple][red]'[/red][red];[/red]

[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url][red]([/red][black][b]my[/b][/black] [blue]$infh[/blue], [red]'[/red][purple]<[/purple][red]'[/red], [blue]$infile[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$infile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black][red]([/red][black][b]my[/b][/black] [blue]$outfh[/blue], [red]'[/red][purple]>[/purple][red]'[/red], [blue]$outfile[/blue][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't open [blue]$outfile[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]

[olive][b]while[/b][/olive] [red]([/red]<[blue]$infh[/blue]>[red])[/red] [red]{[/red]
	[url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
	[black][b]my[/b][/black] [blue]$line[/blue] = [blue]$_[/blue][red];[/red]
	
	[blue]$line[/blue] =~ [red]s/[/red][purple].*id[purple][b]\:[/b][/purple]745[0-9]{4}.*[/purple][red]/[/red][purple]this worked[/purple][red]/[/red][red]sgi[/red][red];[/red]
	
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [blue]$outfh[/blue] [red]"[/red][purple][blue]$line[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[maroon]close[/maroon][red]([/red][blue]$infh[/blue][red])[/red][red];[/red]
[maroon]close[/maroon][red]([/red][blue]$outfh[/blue][red])[/red][red];[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top