I'm trying to make a program that will search files and replace text with user interaction. It looks like everything works fine, but when I cat the file it still has the original text. Any guidance on how to fix?
use warnings; use strict; while (1) { system("clear"); print "Enter P to process a file or Q to quit: "; chomp( my $ans=<STDIN> ); if ( $ans =~ /^p$/i ) { print "What file would you look to open? "; chomp( my $file=<STDIN> ); next if !-e $file; open(my $fh, '<', $file) or die "Could not open file: $!"; print "What text would you like to search for? "; chomp( my $find=<STDIN> ); print "What text would you like it replaced with? "; chomp( my $replace=<STDIN> ); while ( my $line=<$fh> ) { if ( $line =~ /$find/i ) { $line =~ s/\b$find\b/$replace/g; } print $line; } next; } elsif ( $ans =~ /^q$/ ) { exit; } else { next; } }
In reply to replace text by dawg31wh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |