perl pattern matching

Discussion in 'Perl Misc' started by Niko, Aug 31, 2004.

  1. Niko

    Niko Guest

    Hi,

    i need help replacing string in the middle of line like this :


    var1:string:yyy:zzz:kkk.....:ggg


    Need to replace the string.

    I already know the value of var1, that help me to find the specific line in my file.

    only left is to replace the string with a new string.

    any idea ?

    Thanks.
     
    Niko, Aug 31, 2004
    #1
    1. Advertising

  2. Niko

    Paul Lalli Guest

    "Niko" <> wrote in message
    news:...
    > Hi,
    >
    > i need help replacing string in the middle of line like this :
    >
    > var1:string:yyy:zzz:kkk.....:ggg
    >
    > Need to replace the string.
    >
    > I already know the value of var1, that help me to find the specific

    line in my file.
    >
    > only left is to replace the string with a new string.
    >
    > any idea ?



    Your problem statement is much too vague. Where is the string you want
    to replace stored? (ie, in what variable?). With what do you want to
    replace this string? Do you want to replace *all* of the string, or
    just a part of it?

    Whenever possible, post a *short* but *complete* program that
    demonstrates the problem you need to solve. That means actual, real
    Perl code that other people can copy and run.

    Paul Lalli
     
    Paul Lalli, Aug 31, 2004
    #2
    1. Advertising

  3. Niko wrote:
    > Hi,
    >
    > i need help replacing string in the middle of line like this :
    >
    >
    > var1:string:yyy:zzz:kkk.....:ggg
    >
    >
    > Need to replace the string.
    >
    > I already know the value of var1, that help me to find the specific line in my file.
    >
    > only left is to replace the string with a new string.
    >
    > any idea ?
    >
    > Thanks.


    Well this works but it assumes that : is a field delimiter and does not appear
    in the data, specifically it does not appear in the first two fields.

    $x = 'var1:string:yyy:zzz:kkk:ggg';
    @l = split(':', $x);
    $l[1] = 'fred';
    $x = join(':', @l);
    print "$x\n";
     
    Peter Hickman, Aug 31, 2004
    #3
  4. Niko

    J. Gleixner Guest

    Niko wrote:
    > Hi,
    >
    > i need help replacing string in the middle of line like this :
    >
    >
    > var1:string:yyy:zzz:kkk.....:ggg
    >
    >
    > Need to replace the string.
    >
    > I already know the value of var1, that help me to find the specific line in my file.
    >
    > only left is to replace the string with a new string.
    >
    > any idea ?
    >
    > Thanks.


    I'm guessing you want to replace 'string', in your example line above,
    with another value and you know the beginning of the line you want to
    replace, which in this case is 'var1'.

    my $str = 'var1:string:abcd:edfg';
    my $beg = 'var1';
    my $new_string = 'new_value1';

    $str =~ s/^$beg:([^:]*)/$beg:$new_string/;

    print $str, "\n";

    In the future, it'd really help others if there was code or a few lines
    of data, showing cases where you want the line changed and where you
    don't want them changed. That would help find a more accurate solution.

    See ya
     
    J. Gleixner, Aug 31, 2004
    #4
  5. Niko

    Niko Guest

    Hi again,

    First thanks a lot.
    and next time i will put up some line codes to make things much more clear.

    for now this was my code :

    ### Find and replace strings in a file
    sub Find_Rep {
    my $PAT_TXT = $_[0];
    open (FILE,$S_FILE);
    open (TEMPF,"> $T_FILE");
    while (<FILE>) {
    eval $PAT_TXT;
    print TEMPF $_;
    <STDIN>;
    }
    close (TEMPF);
    close (FILE);

    ####
    $S_FILE="source file";
    $T_FILE="target file";
    $str1="var1";
    str2="var2";

    my $PAT_ST = 's/^$str1:([^:]*)/$str1:$str2/';
    Find_Rep($PAT_ST);


    Thanks again.

    Nissim

    "J. Gleixner" <> wrote in message news:<m81Zc.10$>...
    > Niko wrote:
    > > Hi,
    > >
    > > i need help replacing string in the middle of line like this :
    > >
    > >
    > > var1:string:yyy:zzz:kkk.....:ggg
    > >
    > >
    > > Need to replace the string.
    > >
    > > I already know the value of var1, that help me to find the specific line in my file.
    > >
    > > only left is to replace the string with a new string.
    > >
    > > any idea ?
    > >
    > > Thanks.

    >
    > I'm guessing you want to replace 'string', in your example line above,
    > with another value and you know the beginning of the line you want to
    > replace, which in this case is 'var1'.
    >
    > my $str = 'var1:string:abcd:edfg';
    > my $beg = 'var1';
    > my $new_string = 'new_value1';
    >
    > $str =~ s/^$beg:([^:]*)/$beg:$new_string/;
    >
    > print $str, "\n";
    >
    > In the future, it'd really help others if there was code or a few lines
    > of data, showing cases where you want the line changed and where you
    > don't want them changed. That would help find a more accurate solution.
    >
    > See ya
     
    Niko, Sep 1, 2004
    #5
  6. Niko top-posts:

    [ please don't top-post ]

    > "J. Gleixner" <> wrote in message

    news:<m81Zc.10$>...
    >
    >>Niko wrote:
    >>
    >>>i need help replacing string in the middle of line like this :
    >>>
    >>>var1:string:yyy:zzz:kkk.....:ggg
    >>>

    >>
    >>I'm guessing you want to replace 'string', in your example line above,
    >>with another value and you know the beginning of the line you want to
    >>replace, which in this case is 'var1'.
    >>
    >>my $str = 'var1:string:abcd:edfg';
    >>my $beg = 'var1';
    >>my $new_string = 'new_value1';
    >>
    >>$str =~ s/^$beg:([^:]*)/$beg:$new_string/;

    >
    > for now this was my code :
    >
    > ### Find and replace strings in a file
    > sub Find_Rep {
    > my $PAT_TXT = $_[0];


    It is more ideomatic (and faster) to say:

    my $PAT_TXT = shift;

    or

    my ($PAT_TXT) = @_;

    > open (FILE,$S_FILE);
    > open (TEMPF,"> $T_FILE");


    You should always, yes always, check the return value from open(). In
    quick-and-dirty program it is sufficient to append "or die $!".

    You should generally avoid using shared variables to pass arguments to
    subrouitnes unless there is a special reason why it's justified.

    If you use a bare file handle, FILE, in a subroutine you should use
    local(*FILE). Better would be to use lexical file handles (unless you
    need compatability with old Perls).

    > while (<FILE>) {
    > eval $PAT_TXT;


    You should usually check the value of $@ after eval. If you can't think
    of something more appropriate just re-throw with:

    die $@ if $@;

    But you shouldn't be using eval at all anyhow - you should be calling a
    precomplied code entity - see below.

    > print TEMPF $_;
    > <STDIN>;
    > }
    > close (TEMPF);
    > close (FILE);



    I think you have a missing closing curly.

    > ####
    > $S_FILE="source file";
    > $T_FILE="target file";
    > $str1="var1";
    > str2="var2";


    This is an error.

    >
    > my $PAT_ST = 's/^$str1:([^:]*)/$str1:$str2/';
    > Find_Rep($PAT_ST);


    You should use the natural representation for things (i.e. use a CODE
    entity not a string for holding code).

    You should use the natural name for things (i.e. don't use 'PAT' for the
    name of a variable containing a substition command).

    The capture brackets are redunant.

    It would be a good idea to habitually put tThe $str1 on the LHS of the
    s/// in \Q\E just in case it ever contains RE-special punctuation.

    It would be more conventional to preserve the unchanged bit using a
    capture rather the repeat it on each side of the s///.

    my $substitution = sub { s/^(\Q$str1\E:)[^:]*/$1$str2/ };
     
    Brian McCauley, Sep 1, 2004
    #6
    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. seema

    perl pattern matching

    seema, Mar 12, 2005, in forum: Perl
    Replies:
    1
    Views:
    754
    Bob Walton
    Mar 12, 2005
  2. Xah Lee
    Replies:
    9
    Views:
    879
    Chris Smith
    Feb 2, 2005
  3. Xah Lee
    Replies:
    4
    Views:
    639
    Aaron Sherman
    Feb 11, 2005
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    262
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    266
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page