Perl to change a value in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl to change a value in a file
# 1  
Old 02-20-2013
Perl to change a value in a file

Hi, I have a file with the following line

Code:
open (unit=2, file='t0_p0.DAT')

I want to change the value of the above zeros with variables $i and $j using a simple perl one linear.

I've tried the following but it doesn't work

Code:
perl -i -pe "/open/&&s/t\d+_p\d+/t$j_p$i/" file

Thanks!
# 2  
Old 02-20-2013
export the variables and use $ENV{'i'} and $ENV{'j'} in your perl script.
This User Gave Thanks to Subbeh For This Post:
# 3  
Old 02-20-2013
do you mean like
Code:
export i=2
export j=2

and
Code:
perl -i -pe "/open/&&s/t\d+_p\d+/t$ENV{'j'}_p$ENV{'i'}/" interp.f

This just gives

Code:
open (unit=2, file='t{'j'}_p{'i'}.DAT')

# 4  
Old 02-20-2013
Use single quotes to get these variables to work:
Code:
perl -i -pe '/open/&&s/t\d+_p\d+/t$ENV{'j'}_p$ENV{'i'}/' interp.f

This User Gave Thanks to Subbeh 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 Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. Shell Programming and Scripting

Change value in a file using perl or shell script

hi, I have a local.conf file which has the first line TOPDIR = "/home/mvdev/workspace/boxer". I want to replace the value to "/home/common/workspace/mirror". I tried the following perl command that is perl -p -i -e 's/Path/path1/g' myfile.txt then sed... (7 Replies)
Discussion started by: amvarma77
7 Replies

3. Shell Programming and Scripting

Help with Perl to change dhcpd.conf file

Hi all, I am too new for this stuff and i am lost in perl tutorials. I need help to change dhcp entries in .conf file with a perl script. The file entries are like below : host bertha-clp-0 { hardware ethernet AA:0A:A0:00:6c:40; fixed-address 10.10.10.72; option... (6 Replies)
Discussion started by: ekckabatop
6 Replies

4. Shell Programming and Scripting

Perl- Nested 'for' order change

Hello, I'm quite new to perl so my question is rather basic and I know there is probably a simple way around it but I can't seem to find it. I have a medium-length code and there is a part that works with a nested for loop: foreach my $j(@primpiddelta){ for (my $k=1;... (0 Replies)
Discussion started by: acsg
0 Replies

5. Shell Programming and Scripting

Change Directory in Perl

Hi Can any one please support: From Windows, I am running perl script located in C:/scripts directory and need to run a command "ls | sort | uniq" on the files in D:/temp directory. #!c:\perl\bin\perl.exe $file_dir="D:\\temp"; $command1="cd"; $command2="ls | sort | uniq";... (2 Replies)
Discussion started by: sureshcisco
2 Replies

6. Programming

how to change Editor in perl

i have following code that runs fine on my Ubuntu workstation. However when i run this on a HPUX it wont open the text file i am trying to open for editing. I figured i just have to change gvim editor to vi, but that didnt work out well. Can some one help me out here? This is written in Perl ... (1 Reply)
Discussion started by: usustarr
1 Replies

7. Shell Programming and Scripting

how to change time hr:mm to mm:hr in perl

Hi I want to match time hr:mm and change to mm:hr please let me know how to do that in perl (2 Replies)
Discussion started by: lakshmikant
2 Replies

8. Shell Programming and Scripting

change requires with perl

Hi, i need to change a file with perl having contents below, but it will not do any action if change is being already made. file.txt AAAAAA BBBBBB CCCCCC output.txt (that required) AAAAAA #BBBBB DDDDDD CCCCCC Thanks, (2 Replies)
Discussion started by: learnbash
2 Replies

9. Shell Programming and Scripting

Change word in list - PERL

Hi, probably a newbie question..... But there it goes. :o I have a list words. This list is taken out of a file with grep and cut. my @system = (`grep up system_list| cut -d':' -f1` ); system= sm00xp03 sm01ftp02 sm00ssh12 What I want to have is a list, with each of the words in the... (1 Reply)
Discussion started by: pmpx
1 Replies

10. Shell Programming and Scripting

Hw to change the font of output in perl

Hw to change the font color and size of output in perl (2 Replies)
Discussion started by: trupti_rinku
2 Replies
Login or Register to Ask a Question