Complexed serach and replace task, advice?

Discussion in 'Perl' started by Nick Ellson, Nov 20, 2003.

  1. Nick Ellson

    Nick Ellson Guest

    I have search and replace task and was hoping to get some advice. I
    have a large text file of remote access users from Cisco's ACS
    dumpfile that I need to batch alter 2000 of 3000 users. And example
    user record (they are not all the same size)

    #-----------------------------------------------------------------------------
    Name : myuser
    State : 0
    S_flags : 0
    Aging policy : group1
    Good count : 0
    Warning count : 0
    Change count : 0
    Last change Lo: 1648245808
    Last change Hi: 29591352
    Last auth Lo : 0
    Last auth Hi : 0
    Rights : 1
    Type : 256
    EnableType : 4
    Status : 4
    Reset : 1
    Expiry : 302 103 0 4294963983 0 5
    MaxSession : 65535
    MaxSess2 : 0
    Profile : 1
    LogonHrs : 0x0016 00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
    ff ff ff ff ff ff
    Alias : 0
    Value Flags : 524336
    CounterVals_00: 0 0 0 0
    CounterRst_00 : 0 0
    CounterVals_01: 0 0 0 0
    CounterRst_01 : 0 0
    ##--- User End

    I have a simple text file of all the usernames that must be acted on
    already, and i was hoping to find a scripted method of searching this
    file to locate my usernames, then change the TYPE and STATUS lines for
    each respective user to alternate values.

    My problem is how to do a search that will stop at a username, reach
    forward in the file to find the next occurance of TYPE and then
    STATUS, make the replacements, and then continue searching the file
    for the rest of the usernames.

    Doing a bulk replace of all status and type, that's easy with "sed',
    and if i could get Grep to go through a file, lock on a name match,
    and start sed off from that point I might be ok, but I don't see how
    that could be done. So I am betting Perl is my best and perhaps only
    option to do this with minimal developement time. Any text
    manipulators out there see a way to do this from what I have
    described?

    Nick
     
    Nick Ellson, Nov 20, 2003
    #1
    1. Advertising

  2. Nick Ellson

    Guest

    (Nick Ellson) wrote in message news:<>...
    > I have search and replace task and was hoping to get some advice. I
    > have a large text file of remote access users from Cisco's ACS
    > dumpfile that I need to batch alter 2000 of 3000 users. And example
    > user record (they are not all the same size)
    >
    > #-----------------------------------------------------------------------------
    > Name : myuser
    > State : 0
    > S_flags : 0
    > Aging policy : group1
    > Good count : 0
    > Warning count : 0
    > Change count : 0
    > Last change Lo: 1648245808
    > Last change Hi: 29591352
    > Last auth Lo : 0
    > Last auth Hi : 0
    > Rights : 1
    > Type : 256
    > EnableType : 4
    > Status : 4
    > Reset : 1
    > Expiry : 302 103 0 4294963983 0 5
    > MaxSession : 65535
    > MaxSess2 : 0
    > Profile : 1
    > LogonHrs : 0x0016 00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
    > ff ff ff ff ff ff
    > Alias : 0
    > Value Flags : 524336
    > CounterVals_00: 0 0 0 0
    > CounterRst_00 : 0 0
    > CounterVals_01: 0 0 0 0
    > CounterRst_01 : 0 0
    > ##--- User End
    >
    > I have a simple text file of all the usernames that must be acted on
    > already, and i was hoping to find a scripted method of searching this
    > file to locate my usernames, then change the TYPE and STATUS lines for
    > each respective user to alternate values.
    >
    > My problem is how to do a search that will stop at a username, reach
    > forward in the file to find the next occurance of TYPE and then
    > STATUS, make the replacements, and then continue searching the file
    > for the rest of the usernames.


    You don't. Perl is a procedural programing lanuage with variables
    that can change. You simply process the whole file line-wise and note
    user name as it goes past.

    The following is not very efficient but it works:

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

    my %alternate_values =
    (
    myuser => { Type => 'Mad', Status => 'Hungry' },
    );

    my $alt = {};
    while(<>) {
    $alt = $alternate_values{$1} || {} if /^Name\s+:\s+(\S+)/;
    for my $field ( keys %$alt ) {
    s/^($field\s+:\s+)(\S+)/$1$alt->{$field}/g;
    }
    print;
    }

    This newsgroup does not exist (see FAQ). Please do not start threads
    here.
     
    , Nov 25, 2003
    #2
    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. Victor
    Replies:
    0
    Views:
    8,872
    Victor
    Sep 1, 2004
  2. krabhi
    Replies:
    1
    Views:
    9,372
    Marco Meschieri
    Aug 9, 2006
  3. JDT
    Replies:
    8
    Views:
    385
    Mark P
    Oct 20, 2006
  4. Replies:
    2
    Views:
    211
    Yves Dorfsman
    May 23, 2008
  5. Replies:
    20
    Views:
    250
    Tad McClellan
    Mar 4, 2007
Loading...

Share This Page