Store a single character AFTER a match

T

toomanyjoes

Hello I'm trying to edit a text file and after my script finishes save
that text file. One thing I need to do is save the character AFTER a
match into a variable.

while (<FILE>){
s/<(Ra|N\d)>//;
s!^(.\w\w)( \d+):(\d+)!$1~$2~$3!;
if (/<nsup>/)
# Then save the character

I've tried using the $POSTMATCH variable but it grabs the rest of the
line, I only want one character.

Also after I execute my replacements how do I save the changes to the
text file?

while (<FILE>){
s/<(Ra|N\d)>//;
s!^(.\w\w)( \d+):(\d+)!$1~$2~$3!;
}
#save the changes made??
close FILE

Thanks,

Joe




BTW I finally got the correct syntax to open a file!

#!/usr/local/bin/perl -w
use warnings;
use strict;

open FILE, '<', 'C:\gen.txt' or die "Could not open file. $!";
while (<FILE>){
s/<(Ra|N\d)>//;
s!^(.\w\w)( \d+):(\d+)!$1~$2~$3!;
print;
}
close FILE;

Thanks Guys!
 
A

A. Sinan Unur

Hello I'm trying to edit a text file and after my script finishes save
that text file. One thing I need to do is save the character AFTER a
match into a variable.

while (<FILE>){
s/<(Ra|N\d)>//;
s!^(.\w\w)( \d+):(\d+)!$1~$2~$3!;
if (/<nsup>/)
# Then save the character

You'll need to read the posting guidelines and start paying some attention
to the suggestions there.

Post a short and complete script we can run. Provide sample data. Show
what you want to get as well as what your script produces.

The sequence of punctuation and alphanumeric characters above does not do
the job.
BTW I finally got the correct syntax to open a file!

#!/usr/local/bin/perl -w

No need for -w above if you are going to use the warnings pragma.
use warnings;
use strict;

Sinan.
 
B

Brian McCauley

Hello I'm trying to edit a text file and after my script finishes save
that text file. One thing I need to do is save the character AFTER a
match into a variable.

while (<FILE>){
s/<(Ra|N\d)>//;
s!^(.\w\w)( \d+):(\d+)!$1~$2~$3!;
if (/<nsup>/)
# Then save the character

I've tried using the $POSTMATCH variable but it grabs the rest of the
line, I only want one character.

Er, wouldn't that be the first character of $POSTMATCH?

But $POSTMATCH should be avoided for well documented reasons so it would
be better to simply capure the character.

if (my ($char) = /<nsup>(.)/)

(Note: this doesn't match if the next character if it is a newline - if
you want that then you need a switch (looking it up is left as an
exercise for the reader)).
Also after I execute my replacements how do I save the changes to the
text file?

while (<FILE>){
s/<(Ra|N\d)>//;
s!^(.\w\w)( \d+):(\d+)!$1~$2~$3!;
}
#save the changes made??
close FILE

This is FAQ: "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?"

Personally I don't like the terse answer in the latest version of the
FAQ since it no longer mentions the option of writing a replacement file
and renaming it over the original.

See also FAQ "How can I use Perl's -i option from within a program?" and
the CPAN modules Files::Transaction or IO::AtomicFile.
BTW I finally got the correct syntax to open a file!

#!/usr/local/bin/perl -w
use warnings;

You don't need both '-w' and 'warnings'. Get rid of '-w'.
 
M

Michele Dondi

if (/<nsup>/)
# Then save the character
^^^
^^^

_Which_ charachter?
open FILE, '<', 'C:\gen.txt' or die "Could not open file. $!";

Incidentally and in addition to other cmts you've been given, even if
this does work, I recommend using forward slashes '/' anyway, for they
will work just as good and getting used to that practice won't impose
you a mind context switch when writing programs for other OSen.

(But, as a side note to the side note, please note that if you want to
write scripts that are _really_ portable then some more care, and the
use of appropriate modules, is required.)
close FILE;

If you use lexical filehandles you do not need to close() explicitly;
well _generally_ you don't, that is.


Michele
 

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

Forum statistics

Threads
474,166
Messages
2,570,902
Members
47,442
Latest member
KevinLocki

Latest Threads

Top