Complexed serach and replace task, advice?

N

Nick Ellson

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
(e-mail address removed)
 
N

nobull

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top