perl script to replace the text in the original file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl script to replace the text in the original file
# 1  
Old 08-07-2012
perl script to replace the text in the original file

Hi Folks,

I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically.
Code:
<body>
<P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P>
</body>

I have written the below code to replace the string and I am able to print it but how to replace the string(Hello Giridhar) in the html file.

Please correct the code where you think I made the mistakes.

Code:
 
my $filename = "theme1.htm";
my $old = "Hello";
my $new = "Giridhar";
$new = "$old ".$new;
open(my $fh, '<', $file_name) || die "file could not open $! \n";
while( $line = <$fh>) 
{
        if( $line =~ s/$old/$new/g ) 
        {
               print  "$line\n";
        }
}
close($fh);


Thanks in advance...
# 2  
Old 08-07-2012
Code:
my $filename = "theme1.htm";
my $old = "Hello";
my $new = "Giridhar";
$new = "$old ".$new;

$^I = ".orig";
$ARGV[0] = $filename;

while( <> ) 
{
    s/$old/$new/;
    print;
}

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 08-07-2012
lot of thanks bajaje...

For my understanding, Could you please explain the below lines of code..

Code:
$^I = ".orig";
$ARGV[0] = $filename;

Regards,
Giridhar
# 4  
Old 08-07-2012
$^I enables in-file editing. ".orig" will be the extension added to original file and kept in the same directory. So after in-file editing "abc.html" would be retained as "abc.html.orig" in the same directory.

The diamond operator (<>) in while loop iterates over the content in @ARGV. $filename is the first element of array @ARGV.
This User Gave Thanks to balajesuri For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

2. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

3. Shell Programming and Scripting

Shell or perl script to replace XML text in bulk

Hi, I am looking for assistance over shell or perl (without XML twig module) which replace string in XML file under particular branch..example of code file sample.. Exact requirment : Replace "Su saldo es" in below file with "Your balance" but only in XML branch of Text id=98 and Text Id=12... (7 Replies)
Discussion started by: Ashu_099
7 Replies

4. UNIX for Dummies Questions & Answers

Replace string with modifying original file

I have a file abc.txt with following data 121941602P911000497^123.001^^^^^^^^^0^10^OTHER 121941602P911000497^123.002^^^^^^^^^1^10^OTHER 121941602P911000497^123.003^^^^^^^^^2^10^OTHER 121941602P911000497^123.004^^^^^^^^^3^10^OTHER 121941602P911000497^123.005^^^^^^^^^4^10^OTHER I want to... (5 Replies)
Discussion started by: nsuresh316
5 Replies

5. Shell Programming and Scripting

Perl script, replace file with newer file

Hello, Can you please help me one this: I have two servers: Server A and server B. Every day on 03.00AM in only one on these two servers (randomly)is generated one file, lets say file.txt. I want to copy this file also to the other server. I want to create a perl script that does... (2 Replies)
Discussion started by: arrals_vl
2 Replies

6. Shell Programming and Scripting

gawk script to search and replace text in a flat file

Hi I am new to unix and newbie to this forum. I need help in writing a gawk script that search and replace particular text in a flat file. Input file text : ZIDE_CONTROL000 100000000003869920900000300000001ISYNC 000002225489 0000000002232122 20120321 16:40:53 ZIDE_RECORD000... (5 Replies)
Discussion started by: gkausmel
5 Replies

7. UNIX for Dummies Questions & Answers

Replace line via perl script of file

Hi All, I wanted to do following. 1) If file exit then open file for reading and check if my string is there then i wanted to replace the entire line with that string + something else then close the file. 2) If file does not exit then i need to open the file and write to it. I am done with... (0 Replies)
Discussion started by: Tarun24
0 Replies

8. Shell Programming and Scripting

Identify matching data in a file and output to original line, in perl

Hi, I haven't done this for awhile, and further, I've never done it in perl so I appreciate any help you can give me. I have a file of lines, each with 5 data points that look like this: AB,N,ALLIANCEBERNSTEIN HLDNG L.P,AB,N ALD,N,ALLIED CAPITAL CORPORATION,ALD,N AFC,N,ALLIED CAPITAL... (4 Replies)
Discussion started by: Pcushing
4 Replies

9. Shell Programming and Scripting

insert text into the middle of a original file

how do u insert text into a specific place in a file, say the middle for example, without changing the name for that file (1 Reply)
Discussion started by: mopimp
1 Replies

10. Shell Programming and Scripting

script - replace text in file

I have a file something like this (except much larger amount of rows/data) 0001 blue testing1 0002 blue testing2 0006 blue testing3 0232 red testing4 2143 blue testing5 3453 blue testing6 In a script I want to replace the word red with blue where the line begins with a... (4 Replies)
Discussion started by: frustrated1
4 Replies
Login or Register to Ask a Question