How to replace string in only one field which could be duplicated in the file

Discussion in 'Perl Misc' started by waterie@gmail.com, Nov 10, 2005.

  1. Guest

    I have a file consists of serveral sections, and each section consists
    of same/semilar fields. Some fields need to change, the string should
    be replaced without effect same field in other section. Here is an
    example,

    group name = abc # section 1
    stt = 123
    busy = off
    dev = name1
    ssn = 129483021
    dlc = c82u208
    group name = xyz # section 2
    stt = 145
    busy = on
    dev = name2
    ssn = 129432326
    dlc = s43k234
    group name = jhg # section 3
    stt = 321
    busy = off
    dev = name5
    ssn = 198782383
    dlc = d83j208

    # And I need to replace some fields in each section, to have a new file
    like

    group name = abc # section 1
    stt = 321
    busy = on
    dev = fieldx
    ssn = 129483021
    dlc = c82u208
    group name = xxh # section 2
    stt = 145
    busy = on
    dev = fieldy
    ssn = 129432326
    dlc = s43k234
    group name = jhg # section 3
    stt = 321
    busy = off
    dev = name5
    ssn = 238764824
    dlc = d83j143

    Anybody has idea how to do this? I'm new to this kind of replacement,
    really appreciate it!

    Sally
     
    , Nov 10, 2005
    #1
    1. Advertising

  2. Re: How to replace string in only one field which could be duplicatedin the file

    wrote:
    > Anybody has idea how to do this? I'm new to this kind of replacement,
    > really appreciate it!


    What have you tried? Very few will write the code for you. Some will
    help you get _your_ code working.
     
    Andrew McGregor, Nov 10, 2005
    #2
    1. Advertising

  3. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > I have a file consists of serveral sections, and each section consists
    > of same/semilar fields. Some fields need to change, the string should
    > be replaced without effect same field in other section. Here is an
    > example,
    >
    > group name = abc # section 1
    > stt = 123
    > busy = off
    > dev = name1
    > ssn = 129483021
    > dlc = c82u208
    > group name = xyz # section 2
    > stt = 145
    > busy = on
    > dev = name2
    > ssn = 129432326
    > dlc = s43k234
    > group name = jhg # section 3
    > stt = 321
    > busy = off
    > dev = name5
    > ssn = 198782383
    > dlc = d83j208
    >
    > # And I need to replace some fields in each section, to have a new file
    > like
    >
    > group name = abc # section 1
    > stt = 321
    > busy = on
    > dev = fieldx
    > ssn = 129483021
    > dlc = c82u208
    > group name = xxh # section 2
    > stt = 145
    > busy = on
    > dev = fieldy
    > ssn = 129432326
    > dlc = s43k234
    > group name = jhg # section 3
    > stt = 321
    > busy = off
    > dev = name5
    > ssn = 238764824
    > dlc = d83j143


    You expect your readers to compare 18 pairs of nonsensical lines to find
    out where the changes are?

    > Anybody has idea how to do this? I'm new to this kind of replacement,
    > really appreciate it!


    To do what? You haven't explained how to decide what changes to
    make where. You will eventually have to explain that to the program.

    In principle, you'd read each section into a hash whose keys and values
    are the field names and values respectively. Then you inspect the hash
    make any changes that apply (you have yet to specify that part). Then
    write out the changed section to a new file.

    It is unlikely that someone will write the code for you, even with
    complete specifications. Show us what you've tried and we'll help
    you get it right.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Nov 10, 2005
    #3
  4. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > I have a file consists of serveral sections, and each section consists
    > of same/semilar fields. Some fields need to change, the string should
    > be replaced without effect same field in other section. Here is an
    > example,
    >
    > group name = abc # section 1
    > stt = 123
    > busy = off
    > dev = name1
    > ssn = 129483021
    > dlc = c82u208
    > group name = xyz # section 2
    > stt = 145
    > busy = on
    > dev = name2
    > ssn = 129432326
    > dlc = s43k234
    > group name = jhg # section 3
    > stt = 321
    > busy = off
    > dev = name5
    > ssn = 198782383
    > dlc = d83j208
    >
    > # And I need to replace some fields in each section, to have a new file
    > like
    >
    > group name = abc # section 1
    > stt = 321
    > busy = on
    > dev = fieldx
    > ssn = 129483021
    > dlc = c82u208
    > group name = xxh # section 2
    > stt = 145
    > busy = on
    > dev = fieldy
    > ssn = 129432326
    > dlc = s43k234
    > group name = jhg # section 3
    > stt = 321
    > busy = off
    > dev = name5
    > ssn = 238764824
    > dlc = d83j143


    You expect your readers to compare 18 pairs of nonsensical lines to find
    out where the changes are?

    > Anybody has idea how to do this? I'm new to this kind of replacement,
    > really appreciate it!


    To do what? You haven't explained how to decide what changes to
    make where. You will eventually have to explain that to the program.

    In principle, you'd read each section into a hash whose keys and values
    are the field names and values respectively. Then you inspect the hash
    and make any changes that apply (you have yet to specify that part).
    Then write out the changed (or unchanged) section to a new file.

    It is unlikely that someone will write the code for you, even with
    complete specifications. Show us what you've tried and we'll help
    you get it right.

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.

    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Nov 10, 2005
    #4
  5. Guest

    wrote:
    > I have a file consists of serveral sections, and each section consists
    > of same/semilar fields...


    Is it absolutely necessary that your file be in this particular format?
    Do you have control over the format, and could you make a small change
    (to the manner in which you specify the group name) and organize the
    file like as shown in the __DATA__ section of the script below???

    By making this small change, the file looks like a Windows-style ".ini"
    configuration file. Now you have the option to use powerful and easy
    tools to manipulate it. I would investigate Config::IniFiles on CPAN.
    With this module (or one of the many other ".ini" modules) your task
    becomes much simpler. For example:

    use strict; use warnings;
    use Config::IniFiles;

    my %cfg;
    # I'm reading the __DATA__ section. Substitute a filename/handle, etc
    tie %cfg, 'Config::IniFiles', ( -file => *main::DATA )
    or die @Config::IniFiles::errors;

    $cfg{'abc'}{'stt'} = 321; #unclear how this value is obtained
    $cfg{'xyz'}{'dev'} = 'fieldy'; #also unclear
    # ...etc... etc ... etc ...

    tied( %cfg )->WriteConfig( '/tmp/new_config.ini' )
    || die "Could not write settings to new file.";

    __DATA__
    [abc]
    stt = 123
    busy = off
    dev = name1
    ssn = 129483021
    dlc = c82u208
    [xyz]
    stt = 145
    busy = on
    dev = name2
    ssn = 129432326
    dlc = s43k234
     
    , Nov 10, 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. Replies:
    0
    Views:
    431
  2. Replies:
    7
    Views:
    427
    Peter Flynn
    Jun 12, 2008
  3. work4u
    Replies:
    1
    Views:
    140
    Mark Schupp
    Jan 7, 2004
  4. Replies:
    3
    Views:
    118
  5. Tobias Müller
    Replies:
    0
    Views:
    122
    Tobias Müller
    Dec 9, 2013
Loading...

Share This Page