String replace perl script error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String replace perl script error
# 1  
Old 05-04-2006
Data String replace perl script error

I have written down a simple perl program to replace a string from a word file. My script does'nt seem to work. Any pointers to make it work will be appreciated. The code is given below.

Code:
#!/usr/bin/perl

while (<>) {
 open(FILE,$_);
 while (<FILE>) {
        s/This/I did it/g;
        }
 close(FILE);
}

I run the script from the command line as:
perl myscript.pl inputfile.txt
# 2  
Old 05-04-2006
What is your expected results?
# 3  
Old 05-04-2006
This you can do from the command line, from a shell script, or you can put it in a perl script.

$1 is whatever you pass to the script.

Put this command in a file called replace and run it like this
#replace inputfile.txt

This searches the file $1, ($1 is what you pass to the replace script) it looks for the word 'This' and replaces in with 'I did it' and prints it to a new file called /tmp/newinputfile.txt.


perl -e 'while(<>){if(/This/){$_="I did it\n"} print "$_"}' $1 > /tmp/newiputfile.txt

Hope this helps

-X
# 4  
Old 05-04-2006
Re: String replace perl script error

The file, "input.txt" contains the following string:

This is the text I wrote

To start with, I want to replace the word "This" with "I did it".
# 5  
Old 05-04-2006
Here.....This is even better....Since the source file and destination file are the same, consider doing something like this.

commandline
perl -pi -w -e 's/This/I did it/g;' inputfile.txt

script
perl -pi -w -e 's/This/I did it/g;' $1

Run script from commandline.

#myscript.pl inputfile.txt

This is sweet...

-X
# 6  
Old 05-04-2006
I noticed that I the file specified on the command line will be read line by line. I modified the script as below but it still does'nt work. Can there be some problem with file permissions?

Code:
#!/usr/bin/perl

while (<>) {
         s/This/I did it/g;
     }

# 7  
Old 05-04-2006
Code:
#!/usr/bin/perl -i

while (<>) {
        s/This/I\ did\ it/g;
        print;
}

perl -i myscript.pl input.txt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. 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

3. Shell Programming and Scripting

perl- read search and replace string from the file

Dear all, I have a number of files and each file has two sections separated by a blank line. At the top section, I have lines which describes the values of the alphabetical characters, # s #; 0.123 # p #; 12.3 # d #; -2.33 # f #; 5.68 <blank line> sssssss spfdffff sdfffffff Now I... (4 Replies)
Discussion started by: sasharma
4 Replies

4. Shell Programming and Scripting

Date format check and replace string in PERL

I have got few date format patterns like "yyyymmdd", "yy_mm_dd" etc. There can be any combination of such patterns. I have used add_delta_days to find "yyyy", "yy", "mm", "dd" for the current date and saved them to different variables like "$y1", "$y2", "$m1" etc In one line, i want to... (10 Replies)
Discussion started by: irudayaraj
10 Replies

5. Shell Programming and Scripting

replace a string in perl - Urgent

Hi, Can anyone help me to replace ='is NOT NULL' to is NOT NULL in perl. I tried all the methods which i know, i didnt arrive at the solution. please help!! ---------- Post updated at 03:01 PM ---------- Previous update was at 02:56 PM ---------- I got it.......! Thanks, (1 Reply)
Discussion started by: jam_prasanna
1 Replies

6. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

7. Shell Programming and Scripting

How to replace string in perl?

Hi, I have string like this: $query="#1,apple"; $string=$query; I want to replace #1 with fruit. I tried like this: string=~s/#\d+/$query/ig; print "\n string: $string\n"; It is working only when there is single #1 or #2 but when i give like #1,#2,#3,apple the above code... (2 Replies)
Discussion started by: vanitham
2 Replies

8. Shell Programming and Scripting

Script to replace numbers by string

Hi! I need the following script: - All numbers in a filename (0-9) have to be replace by a String ("Zero"-"Nine") - The script has to go through all the files in the current directory and has to replace the numbers as described above... I have no idea how to do this... Thanks! Michael (5 Replies)
Discussion started by: Michi21609
5 Replies

9. Shell Programming and Scripting

PERL: Replace multiple objects within a string

looking to replace parameters within a string with an external answer - with multiple replacements within a string %% will be used to wrap the objects to be replaced i.e. hello %%title%% %%user%% from %%address%% you last %%action%% on %%object%% the params will be used to make calls to a... (1 Reply)
Discussion started by: bidbrooken
1 Replies

10. Shell Programming and Scripting

How to search a date format from a file an replace with a string in PERL

I am very new to Perl. I am struggling so hard to search a date (such as 10/09/2009, 10-09-2009) from a text file and replace with a string (say DATE) using Perl. Please help me out. Thanks in advance. Regds Doren (4 Replies)
Discussion started by: my_Perl
4 Replies
Login or Register to Ask a Question