I have the following script that takes an input file, an output file and replaces the line in the input file with some other line and writes the output file.
I want to change the script to go through the file directory, that is, instead of asking for input and output files, the script should take as an argument the path to the directory, such as C: \ temp \ allFilesTobeReplaced \ and find the line x and replace it with y for all files under this path to the directory and write out the same files.
How to do it?
Thanks.
$file=$ARGV[0]; open(INFO,$file); @lines=<INFO>; print @lines; open(INFO,">c:/filelist.txt"); foreach $file (@lines){ #print "$file\n"; print INFO "$file"; } #print "Input file name: "; #chomp($infilename = <STDIN>); if ($ARGV[0]){ $file= $ARGV[0] } print "Output file name: "; chomp($outfilename = <STDIN>); print "Search string: "; chomp($search = <STDIN>); print "Replacement string: "; chomp($replace = <STDIN>); open(INFO,$file); @lines=<INFO>; open(OUT,">$outfilename") || die "cannot create $outfilename: $!"; foreach $file (@lines){ # read a line from file IN into $_ s/$search/$replace/g; # change the lines print OUT $_; # print that line to file OUT } close(IN); close(OUT);
replace perl bulk
shubster
source share