How to write a program to ...

G

gjena1

I'm new to perl. Would someone please give me an example
of a perl program to read from a fileA which has several
datas, and search fileA for string1 and change it to string2
in fileA. string1 is located in various places in each
line. Find the the string1 that is located 10 spaces from
the beginning of each line in fileA.
Any suggestions would be greatly greatly appreciated.

gjena1
 
G

Gunnar Hjalmarsson

gjena1 said:
I'm new to perl. Would someone please give me an example
of a perl program to read from a fileA which has several
datas, and search fileA for string1 and change it to string2
in fileA. string1 is located in various places in each
line. Find the the string1 that is located 10 spaces from
the beginning of each line in fileA.

If you want somebody to write a script for you, hire a consultant.

If you want to learn how to program in Perl, read a beginners book
and/or tutorial, do some exercises, etc. This is a good starting-point:

http://learn.perl.org/

Good luck!
 
T

Tad McClellan

gjena1 said:
Subject: How to write a program to ...


Please put the subject of your article in the Subject of your article.

Would someone please give me an example
of a perl program to read from a fileA which has several
datas, and search fileA for string1 and change it to string2
in fileA.


Your Question is Asked Frequently.

perldoc -q change

How do I change one line in a file/delete a line in a file/insert a
line in the middle of a file/append to the beginning of a file?

string1 is located in various places in each
line. Find the the string1 that is located 10 spaces from
the beginning of each line in fileA.


"various places" or that particular 10-space place?

Which is it?

This will make the substitutions anywhere on a line:

--------------------------------
#!/usr/bin/perl
use warnings;
use strict;
use Tie::File;

tie my @array, 'Tie::File', 'fileA' or die "tie failed $!";
s/string1/string2/g for @array;
untie @array;
--------------------------------

Any suggestions would be greatly greatly appreciated.


Check the Perl FAQ *before* posting to the Perl newsgroup.
 
J

John W. Krahn

gjena1 said:
I'm new to perl.

Welcome to Perl. Perhaps as a beginner you might be more comfortable with the
beginners mailing list: http://lists.perl.org/showlist.cgi?name=beginners

Would someone please give me an example
of a perl program to read from a fileA which has several
datas, and search fileA for string1 and change it to string2
in fileA. string1 is located in various places in each
line. Find the the string1 that is located 10 spaces from
the beginning of each line in fileA.
Any suggestions would be greatly greatly appreciated.


#!/usr/bin/perl
use warnings;
use strict;

my $file = 'fileA';

open my $FH, '<', $file or die "Cannot open $file: $!";

while ( my $line = <$FH> ) { # read from a fileA

# search fileA for string1 and change it to string2
$line =~ s/string1/string2/g;

# Find the the string1 that is located 10 spaces from
# the beginning of each line in fileA
if ( substr( $line, 10, length( 'string1' ) ) eq 'string1' ) {
...
}

}

close $FH or die "Cannot close $file: $!";

__END__



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top