How to replace a value in a file in perl?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace a value in a file in perl?
# 1  
Old 09-09-2010
How to replace a value in a file in perl?

Hi,

I have a file with the following contents and i want to replace that value in a file with some other value.

Code:
#file.txt

/local/Disk/data
n
192.168.55.98
52035
3
1
2
1
1
192.168.55.98

Here is my code.
Code:
open(FH,"file.txt");
@array=<FH>;
print "\n array[7]\n";


I want to replace the $array[7] (i.e 1) with 2 in a file.

The output should be like this:
Code:
/local/Disk/data
n
192.168.55.98
52035
3
1
2
2
1
192.168.55.98

How can i do it in perl?

Regards
Vanitha
# 2  
Old 09-09-2010
For infile replacement -

Code:
$
$
$ cat -n f10
     1  /local/Disk/data
     2  n
     3  192.168.55.98
     4  52035
     5  3
     6  1
     7  2
     8  1
     9  1
    10  192.168.55.98
$
$
$ perl -pi.bak -le '$.==8 && s/$_/2/' f10
$
$ cat -n f10
     1  /local/Disk/data
     2  n
     3  192.168.55.98
     4  52035
     5  3
     6  1
     7  2
     8  2
     9  1
    10  192.168.55.98
$
$
$ cat -n f10.bak
     1  /local/Disk/data
     2  n
     3  192.168.55.98
     4  52035
     5  3
     6  1
     7  2
     8  1
     9  1
    10  192.168.55.98
$
$
$

tyler_durden
# 3  
Old 09-10-2010
Quote:
Originally Posted by durden_tyler
For infile replacement -

Code:
$
$
$ cat -n f10
     1  /local/Disk/data
     2  n
     3  192.168.55.98
     4  52035
     5  3
     6  1
     7  2
     8  1
     9  1
    10  192.168.55.98
$
$
$ perl -pi.bak -le '$.==8 && s/$_/2/' f10
$
$ cat -n f10
     1  /local/Disk/data
     2  n
     3  192.168.55.98
     4  52035
     5  3
     6  1
     7  2
     8  2
     9  1
    10  192.168.55.98
$
$
$ cat -n f10.bak
     1  /local/Disk/data
     2  n
     3  192.168.55.98
     4  52035
     5  3
     6  1
     7  2
     8  1
     9  1
    10  192.168.55.98
$
$
$

tyler_durden
Hi,

Thanks for the reply but i am not able to execute i am getting syntax error is there any other way then perl one liners?

Code:
Can't find string terminator "'" anywhere before EOF at -e line 1.

Regards
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. UNIX for Advanced & Expert Users

Perl command to replace word in file...

Hi, I want to search for a specific word in file and replace whole line with new text. e.g. 1) I have file with below lines APP=ABCD 12/12/2012 DB=DDB 01/01/2013 I need perl command which will check for APP=$VAL and replace whole line with APP=$NEWVAL $NEWDT Simlarly need a... (2 Replies)
Discussion started by: mgpatil31
2 Replies

3. Shell Programming and Scripting

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. <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... (3 Replies)
Discussion started by: giridhar276
3 Replies

4. Shell Programming and Scripting

New to Perl Mail @ sign search and replace in a file

HI I'm terribly new to perl .. I;ve been trying to use this command to search and replace entries in a file I tried this and it works perl -p -i -e 's/old/new/' filename Problem is that I have a list of email addresses and I need to serach and replace the entire email address as my... (5 Replies)
Discussion started by: mnassiri
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. 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

7. Shell Programming and Scripting

Perl how to replace e-mail address from the file

I have a script which updates the users e-mail address according to wherever the users type in the browser. The script does other stuffs but this what i am struggling with ..lol. Basically, we are using the command below to try to update the e-mail, however since the e-mail address has "@" the perl... (2 Replies)
Discussion started by: cacm1975
2 Replies

8. Shell Programming and Scripting

Perl search and replace file content.

I am not sure if this is doable. I am trying to open and print the content of the file by replacing all instances fo perl to PERL . This is my code but it is giving me the number count instead of the actual lines with changes. open (PERLHISTORY, 'sample.txt') or die "The file sample.txt could... (3 Replies)
Discussion started by: jxh461
3 Replies

9. Shell Programming and Scripting

Find/replace to new file: ksh -> perl

I have korn shell script that genretaets 100 file based on template replacing the number. The template file is as below: $ cat template file number: NUMBER The shell script is as below: $ cat gen.sh #!/bin/ksh i=1; while ((i <= 100)); do sed "s/NUMBER/$i/" template > file_${i} ((... (1 Reply)
Discussion started by: McLan
1 Replies

10. Shell Programming and Scripting

replace space with delimiter in whole file -perl

Hi I have a file which have say about 100,000 records.. the records in it look like Some kind of text 1234567891 abcd February 14, 2008 03:58:54 AM lmnop This is how it looks.. if u notice there is a 2byte space between each column.. and im planning to replace that with '|' .. ... (11 Replies)
Discussion started by: meghana
11 Replies
Login or Register to Ask a Question