Variable/multi-line substitution

D

droman

Hello,

I'm trying to write a script to do a fairly complicated substitution in
a large number of text files. The third line of each text file reads
something like
"1995-09-25S.IRIG_CHT 6".

The '1995-09-25S.' part of this line is different in each file, but the
rest of the line is identical in all files. What I need to do is add
(as the forth, fifth, etc. lines) several lines to each file in which
the '1995-09-25S.' is the same on each line but the 'IRIG_CHT' part is
different, so that from the third line the file reads
"1995-09-25S.IRIG_CHT 6
1995-09-25S.MBET_CHT 6
1995-09-25S.MGAT_CHT 6
..."

I'm trying to do this as a substitution. So for each file, I read the
'1995-09-25S.' part of the third line into a variable ($date) and then
I try to substitute as follows:

s/$dateIRIG_CHT 6/$dateIRIG_CHT 6\n$dateMBET_CHT 6\n.../i;

Obviously, I'm a Perl newbie and am getting the syntax wrong or using
substitution for too difficult of a task, because I can't get it to
work. Is it possible to use substitution in Perl to substitute long,
multi-line strings containing a combination of text and variables? If
so, any ideas on what I've done wrong?

Thanks much.
 
I

Ian Wilson

(e-mail address removed) wrote:

I try to substitute as follows:

s/$dateIRIG_CHT 6/$dateIRIG_CHT 6\n$dateMBET_CHT 6\n.../i;

$dateIRIG_CHT is a valid variable name and , in your case, is likely to
have no value set for it. You can use curly brackets to clarify what you
want thus: ${date}IRIG_CHT

Try
use strict;
use warnings;
as I believe is suggested in the posting guidelines for this newsgroup.
This ought to catch this sort of error for you.
Obviously, I'm a Perl newbie and am getting the syntax wrong or using
substitution for too difficult of a task, because I can't get it to
work.

I suspect you could simplify your code by making better use of regular
expressions in substitutions - you may not need a separate $date
variable at all.
 
T

Tad McClellan

Ian Wilson said:
(e-mail address removed) wrote:



$dateIRIG_CHT is a valid variable name and , in your case, is likely to
have no value set for it. You can use curly brackets to clarify what you
want thus: ${date}IRIG_CHT


Even better (IMO), would be to not include the dot in $date, then:

s/$date\.IRIG_CHT 6/$date.IRIG_CHT 6\n$date.MBET_CHT 6\n.../i;
 
T

Tad McClellan

I'm trying to write a script to do a fairly complicated substitution in
a large number of text files. The third line of each text file reads
something like
"1995-09-25S.IRIG_CHT 6".

The '1995-09-25S.' part of this line is different in each file, but the
rest of the line is identical in all files. What I need to do is add
(as the forth, fifth, etc. lines) several lines to each file in which
the '1995-09-25S.' is the same on each line but the 'IRIG_CHT' part is
different, so that from the third line the file reads
"1995-09-25S.IRIG_CHT 6
1995-09-25S.MBET_CHT 6
1995-09-25S.MGAT_CHT 6
s/$dateIRIG_CHT 6/$dateIRIG_CHT 6\n$dateMBET_CHT 6\n.../i;

Obviously, I'm a Perl newbie


Have you seen the Posting Guidelines that are posted here frequently?

If you had (and had followed the "Ask perl to help you" suggestion ),
then perl itself would have pointed out the problem to you right away.

That is exactly the reason that that guideline is in there. :)

Is it possible to use substitution in Perl to substitute long,
multi-line strings containing a combination of text and variables?


Yes, but you do not need that in order to solve the problem you've
outlined, see below.

so, any ideas on what I've done wrong?


It is Really Nice if you provide a short and complete program,
that we can run, that illustrates the problem you are having.

Here's a short and complete program that *you* can run to see
the solution, for example:

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

while ( <DATA> ) {
if ( $. == 3 and m/^(\d{4}-\d\d-\d\d[A-Z])\.IRIG_CHT( 6\n)/i ) {
foreach my $type ( qw/MBET_CHT MGAT_CHT/ ) {
$_ .= "$1.$type$2";
}
}
print;
}

__DATA__
first
second
1995-09-25S.IRIG_CHT 6
fourth
 
D

droman

Thank you all - it's working correctly now and I see what I was doing
wrong. Apologies for not reading the Posting Guidlines first - I've
read them now and will post accordingly in the future.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top