Skip to main content

Posts

Showing posts from May, 2013

Perl Script to Replace string in a file

#!/usr/bin/perl #Script:- This Script Replaces the string with given string #Author:- Surendra Patil #Date:- 04/04/2013 use strict; use warnings; my $line = undef; #open the file in reading and writing mode open(INPUT_FILE,”+<names.txt”); #store the contents of the file into memory my @data = <INPUT_FILE>; #set the file pointer to the start of the file seek INPUT_FILE,0,0; #loop and replace the string with ignoring case foreach $line (@data) { $line =~ s/belgaum/california/ig; print INPUT_FILE $line; } close INPUT_FILE; #display the modified file open(INPUT_FILE,”<names.txt”); while (my $line = <INPUT_FILE>) { print “$line”; } close INPUT_FILE; =================================================== Input file:- string.txt Surendra Patil From Nidasoshi  belgaum       Karnataka Bangalore India ==================================================== Output:- Surendra Patil From Nidasoshi  california       Karnataka Bangalore India