Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Variable/multi-line substitution

Reply
Thread Tools

Variable/multi-line substitution

 
 
droman@earth.leeds.ac.uk
Guest
Posts: n/a
 
      06-22-2005
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.

 
Reply With Quote
 
 
 
 
Ian Wilson
Guest
Posts: n/a
 
      06-22-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

<snip: description of objective>

>
> 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.
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      06-22-2005
Ian Wilson <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
>
><snip: description of objective>
>
>> 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



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;


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      06-22-2005
(E-Mail Removed) <(E-Mail Removed)> wrote:

> 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
--------------------------------



--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
droman@earth.leeds.ac.uk
Guest
Posts: n/a
 
      06-23-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Should this substitution be compilable? valentin tihomirov VHDL 12 11-30-2004 03:44 PM
Substitution Problem Ashok Perl 1 07-18-2004 09:33 PM
adobe multiline substitution Justin Perl 0 12-08-2003 08:28 PM
Q: string substitution in a file Troll Perl 6 09-26-2003 01:50 PM
Converted to Mozilla but one thing missing - key macro/substitution no-spam Firefox 5 07-29-2003 08:07 PM



Advertisments