Edit a file in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Edit a file in perl
# 1  
Old 06-13-2014
Lightbulb Edit a file in perl

Hi,

I have a file like
Code:
$ cat abc
HDR XXX
content XXX
content YYY
content XXX
content YYY
content XXX
content YYY
TRL YYY

I want to replace the lines staritng with HDR and TRL
For this I have written below code
Code:
#!/usr/bin/perl -w

use strict;

open ( FH , "+< abc" ) || die "Can't open abc $!";

while(<FH>){
        if (/^HDR/){
                s/XXX/YYY/; print FH;
        }elsif(/^TRL/){
                s/YYY/XXX/; print FH;
        }
}

close (FH);

However on runnig this I end up with something like
Code:
$ cat abc
HDR XXX
HDR YYY
XXX
content YYY
content XXX
content YYY
content XXX
content YYY
TRL YYY
TRL XXX

Help!!!
(answers in perl only)
# 2  
Old 06-13-2014
In your logic every line is printed, then some (starting with "HDR" or "TRL") are changed and then printed again.

You need to switch off the automatic echoing of lines, then move the "print FH" out of the if- and elsif-branches, like this:

Code:
while(<FH>){
        if (/^HDR/){
                s/XXX/YYY/;
        }elsif(/^TRL/){
                s/YYY/XXX/;
        }
        print FH;
}

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Edit distance using perl or awk

Dear all, I am working on a large Sindhi lexicon which I hope to complete by 2017 and place in open source. The database is in Arabic script in two columns delimited by an equal to sign. Column 1 contains a word or words without the short vowel and also some extraneous information which is... (0 Replies)
Discussion started by: gimley
0 Replies

2. Shell Programming and Scripting

perl edit file

Is there a way to edit a file without opening two files the only method I know is one file for reading from and one file writing to I cannot think of any other ways (4 Replies)
Discussion started by: 3junior
4 Replies

3. Programming

Problems using Perl DBI to edit database entries - basic stuff

Hello. I am taking a Perl class in college and we've briefly covered SQL and moved on. We have a term project and we can do whatever we want. My project will rely strongly on an SQL Database so I am trying to learn as much about Perl DBI as I can to get things up and going. I am basically... (1 Reply)
Discussion started by: Dave247
1 Replies

4. Shell Programming and Scripting

Script to Edit the file content and create new file

I have a requirement, which is as follows *. Folder contains list of xmls. Script has to create new xml files by copying the existing one and renaming it by appending "_pre.xml" at the end. *. Each file has multiple <Name>fileName</Name> entry. The script has to find the first occurance of... (1 Reply)
Discussion started by: sudesh.ach
1 Replies

5. Shell Programming and Scripting

help on a perl script to edit file

Hi, sample file looks like this.. <hp> <name> <detail>adsg</detail> ... ... </name><ft>4264</ft> </hp> I need to edit the last but one line using perl script. I want the format to be .. <hp> <name> <detail>adsg</detail> ... ... </name> (9 Replies)
Discussion started by: meghana
9 Replies

6. Shell Programming and Scripting

Edit value in File

I have a file oratab with entry like this SCADAG:/esitst1/oracle/product/9.2.0.8:Y I am trying to discover a way to change the 9.2.0.8 part of this to something like 10.2.0.4 as part of an upgrade script. I have tried cat /etc/oratab >>/tmp/oratab... (1 Reply)
Discussion started by: sewood
1 Replies

7. Shell Programming and Scripting

Edit a line in a file with perl

Hi, How can I edit a line in a file? For example, a.txt contains: start: 1 2 3 4 stop: a b c d and I want to change "3" to "9" and to add "5" after "4" the result should be (a.txt): start: 1 9 3 4 5 stop: a b c d Thanks, zed (5 Replies)
Discussion started by: zed
5 Replies

8. Shell Programming and Scripting

Shell/Perl Script to edit dhcpd.conf

Hi, I need to get a script together to edit the dhcp service configuration file dhcpd.conf. Mac addresses are defined in classes ex. class "HOST1" { match if substring (hardware, 1,18)=00:11:11:FF:FF:FF;} class "HOST2" ... class "HOST3" ... ... followed by allow or deny statements:... (4 Replies)
Discussion started by: sahilb
4 Replies

9. Shell Programming and Scripting

file edit help

Hi, Could anyone give me a idea how to strip the lines from a given file. example *********** 1st occurence 1st occurence 1st occurence 1st occurence *********** 2nd occurence 2nd occurence 2nd occurence 2nd occurence 2nd occurence 2nd occurence ************* 3rd occurence 3rd... (10 Replies)
Discussion started by: sentak
10 Replies

10. Shell Programming and Scripting

Perl CGI to access / edit "root" owned config files

I am trying to write a CGI program which accesses UNIX configuration files and changes them as required. The thing is, I don't want the CGI program to be "root" owned - it's Perl based! Is there any way that the Perl CGI program can request a username and password - and then use this to... (1 Reply)
Discussion started by: WIntellect
1 Replies
Login or Register to Ask a Question