Variable/multi-line substitution

Discussion in 'Perl Misc' started by droman@earth.leeds.ac.uk, Jun 22, 2005.

  1. Guest

    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.
     
    , Jun 22, 2005
    #1
    1. Advertising

  2. Ian Wilson Guest

    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.
     
    Ian Wilson, Jun 22, 2005
    #2
    1. Advertising

  3. Ian Wilson <> wrote:
    > 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 22, 2005
    #3
  4. <> 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 22, 2005
    #4
  5. Guest

    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.
     
    , Jun 23, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dean
    Replies:
    5
    Views:
    496
    Phlip
    Jun 4, 2006
  2. bernadou

    Force multi line field value to output with line breaks?

    bernadou, Jan 22, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    391
    bernadou
    Jan 23, 2006
  3. Anthony Papillion
    Replies:
    0
    Views:
    139
    Anthony Papillion
    Sep 2, 2013
  4. Chris “Kwpolska†Warrick

    Re: How can I remove the first line of a multi-line string?

    Chris “Kwpolska†Warrick, Sep 2, 2013, in forum: Python
    Replies:
    0
    Views:
    120
    Chris “Kwpolska†Warrick
    Sep 2, 2013
  5. Oscar Benjamin
    Replies:
    0
    Views:
    125
    Oscar Benjamin
    Sep 2, 2013
Loading...

Share This Page