Script to modify NIS maps

Script to modify NIS maps

am 18.09.2007 15:50:45 von littlehelphere

Does anyone know of a script or method to remove users from NIS maps
via a script. I have been trying to accomplish this via sed, Perl ,
etc but cannot get it to work. The main issue is each NIS maps has a
different format. Does anyone know of one Perl command, or any other
means, that would accomplish this? Thanks.

Re: Script to modify NIS maps

am 19.09.2007 16:38:41 von littlehelphere

On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
> Does anyone know of a script or method to remove users from NIS maps
> via a script. I have been trying to accomplish this via sed, Perl ,
> etc but cannot get it to work. The main issue is each NIS maps has a
> different format. Does anyone know of one Perl command, or any other
> means, that would accomplish this? Thanks.

Anyone know of anything that can help me accomplish this?

Re: Script to modify NIS maps

am 19.09.2007 17:17:53 von William James

On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
> On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
> > Does anyone know of a script or method to remove users from NIS maps
> > via a script. I have been trying to accomplish this via sed, Perl ,
> > etc but cannot get it to work. The main issue is each NIS maps has a
> > different format. Does anyone know of one Perl command, or any other
> > means, that would accomplish this? Thanks.
>
> Anyone know of anything that can help me accomplish this?

Give us a small sample of an NIS map.

Re: Script to modify NIS maps

am 19.09.2007 17:55:00 von littlehelphere

On Sep 19, 11:17 am, William James wrote:
> On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
>
> > On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
> > > Does anyone know of a script or method to remove users from NIS maps
> > > via a script. I have been trying to accomplish this via sed, Perl ,
> > > etc but cannot get it to work. The main issue is each NIS maps has a
> > > different format. Does anyone know of one Perl command, or any other
> > > means, that would accomplish this? Thanks.
>
> > Anyone know of anything that can help me accomplish this?
>
> Give us a small sample of an NIS map.

William,
There are numerous maps I would be looking to edit via the tool/
script. Such maps as netgroup, group,

i.e - I want to remove foo3 from all maps below. You see the format
and placement will vary on different maps.

group: foousers:*:9999:foo1,foo2,foo4,foo3
netgroup: (,foo1,) (,foo2,) (,foo3,) (,foo4,)
printers: foo3 hp11

Re: Script to modify NIS maps

am 19.09.2007 19:47:47 von William James

On Sep 19, 10:55 am, littlehelph...@gmail.com wrote:
> On Sep 19, 11:17 am, William James wrote:
>
> > On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
>
> > > On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
> > > > Does anyone know of a script or method to remove users from NIS maps
> > > > via a script. I have been trying to accomplish this via sed, Perl ,
> > > > etc but cannot get it to work. The main issue is each NIS maps has a
> > > > different format. Does anyone know of one Perl command, or any other
> > > > means, that would accomplish this? Thanks.
>
> > > Anyone know of anything that can help me accomplish this?
>
> > Give us a small sample of an NIS map.
>
> William,
> There are numerous maps I would be looking to edit via the tool/
> script. Such maps as netgroup, group,
>
> i.e - I want to remove foo3 from all maps below. You see the format
> and placement will vary on different maps.
>
> group: foousers:*:9999:foo1,foo2,foo4,foo3
> netgroup: (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> printers: foo3 hp11

# Produces array of nonmatching and matching
# substrings. The size of the array will
# always be an odd number. The first and the
# last item will always be nonmatching.
function shatter( s, shards, regexp )
{ gsub( regexp, "\1&\1", s )
return split( s, shards, "\1" )
}

BEGIN { target = "foo3" }

{
size = shatter( $0, array, "[:,()]+" )
if (1 == size)
shatter( $0, array, "[ \t]+" )
out = ""
p = 0
for (i=1; i in array; i++)
if ( array[i] == target )
{ p = i
break
}
if (0 == p)
out = $0
else
for (i=1; i in array; i++)
{
if (i == p-1 )
{ if ( "(," == array[i] )
continue
}
else if ((i == p) || (i == p+1) )
continue
out = out array[i]
}
print out
}

==== input ====
foousers:*:9999:foo1,foo2,foo4,foo3
(,foo1,) (,foo2,) (,foo3,) (,foo4,)
(,foo3,)
foo3 hp11
bar foo3 hp11
foo3
==== output ====
foousers:*:9999:foo1,foo2,foo4,
(,foo1,) (,foo2,) (,foo4,)

hp11
bar hp11

Re: Script to modify NIS maps

am 19.09.2007 21:18:01 von littlehelphere

On Sep 19, 1:47 pm, William James wrote:
> On Sep 19, 10:55 am, littlehelph...@gmail.com wrote:
>
>
>
> > On Sep 19, 11:17 am, William James wrote:
>
> > > On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
>
> > > > On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
> > > > > Does anyone know of a script or method to remove users from NIS maps
> > > > > via a script. I have been trying to accomplish this via sed, Perl ,
> > > > > etc but cannot get it to work. The main issue is each NIS maps has a
> > > > > different format. Does anyone know of one Perl command, or any other
> > > > > means, that would accomplish this? Thanks.
>
> > > > Anyone know of anything that can help me accomplish this?
>
> > > Give us a small sample of an NIS map.
>
> > William,
> > There are numerous maps I would be looking to edit via the tool/
> > script. Such maps as netgroup, group,
>
> > i.e - I want to remove foo3 from all maps below. You see the format
> > and placement will vary on different maps.
>
> > group: foousers:*:9999:foo1,foo2,foo4,foo3
> > netgroup: (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > printers: foo3 hp11
>
> # Produces array of nonmatching and matching
> # substrings. The size of the array will
> # always be an odd number. The first and the
> # last item will always be nonmatching.
> function shatter( s, shards, regexp )
> { gsub( regexp, "\1&\1", s )
> return split( s, shards, "\1" )
>
> }
>
> BEGIN { target = "foo3" }
>
> {
> size = shatter( $0, array, "[:,()]+" )
> if (1 == size)
> shatter( $0, array, "[ \t]+" )
> out = ""
> p = 0
> for (i=1; i in array; i++)
> if ( array[i] == target )
> { p = i
> break
> }
> if (0 == p)
> out = $0
> else
> for (i=1; i in array; i++)
> {
> if (i == p-1 )
> { if ( "(," == array[i] )
> continue
> }
> else if ((i == p) || (i == p+1) )
> continue
> out = out array[i]
> }
> print out
>
> }
>
> ==== input ====
> foousers:*:9999:foo1,foo2,foo4,foo3
> (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> (,foo3,)
> foo3 hp11
> bar foo3 hp11
> foo3
> ==== output ====
> foousers:*:9999:foo1,foo2,foo4,
> (,foo1,) (,foo2,) (,foo4,)
>
> hp11
> bar hp11

William,
Thanks for the info. This seems like a good start to what I need.
However, I have two questions for you
1) For the section "BEGIN { target = "foo3" }" how do I see this up
to take info from a for loop or from a file? Do I use an OPEN
statement?
2) The above output does the job on the netgroups and the groups.
However, I would like to have the entire entry for foobar3 removed
from the other maps. In these maps the foobar entry is on a line by
itself. Is this a situation for a case style statement? One for
netgroups, one for groups, one for printers, etc or can this be
accomplished in another manner?
Thanks again for your help.

Re: Script to modify NIS maps

am 19.09.2007 21:32:48 von William James

On Sep 19, 2:18 pm, littlehelph...@gmail.com wrote:
> On Sep 19, 1:47 pm, William James wrote:
>
> > On Sep 19, 10:55 am, littlehelph...@gmail.com wrote:
>
> > > On Sep 19, 11:17 am, William James wrote:
>
> > > > On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
>
> > > > > On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
> > > > > > Does anyone know of a script or method to remove users from NIS maps
> > > > > > via a script. I have been trying to accomplish this via sed, Perl ,
> > > > > > etc but cannot get it to work. The main issue is each NIS maps has a
> > > > > > different format. Does anyone know of one Perl command, or any other
> > > > > > means, that would accomplish this? Thanks.
>
> > > > > Anyone know of anything that can help me accomplish this?
>
> > > > Give us a small sample of an NIS map.
>
> > > William,
> > > There are numerous maps I would be looking to edit via the tool/
> > > script. Such maps as netgroup, group,
>
> > > i.e - I want to remove foo3 from all maps below. You see the format
> > > and placement will vary on different maps.
>
> > > group: foousers:*:9999:foo1,foo2,foo4,foo3
> > > netgroup: (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > > printers: foo3 hp11
>
> > # Produces array of nonmatching and matching
> > # substrings. The size of the array will
> > # always be an odd number. The first and the
> > # last item will always be nonmatching.
> > function shatter( s, shards, regexp )
> > { gsub( regexp, "\1&\1", s )
> > return split( s, shards, "\1" )
>
> > }
>
> > BEGIN { target = "foo3" }
>
> > {
> > size = shatter( $0, array, "[:,()]+" )
> > if (1 == size)
> > shatter( $0, array, "[ \t]+" )
> > out = ""
> > p = 0
> > for (i=1; i in array; i++)
> > if ( array[i] == target )
> > { p = i
> > break
> > }
> > if (0 == p)
> > out = $0
> > else
> > for (i=1; i in array; i++)
> > {
> > if (i == p-1 )
> > { if ( "(," == array[i] )
> > continue
> > }
> > else if ((i == p) || (i == p+1) )
> > continue
> > out = out array[i]
> > }
> > print out
>
> > }
>
> > ==== input ====
> > foousers:*:9999:foo1,foo2,foo4,foo3
> > (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > (,foo3,)
> > foo3 hp11
> > bar foo3 hp11
> > foo3
> > ==== output ====
> > foousers:*:9999:foo1,foo2,foo4,
> > (,foo1,) (,foo2,) (,foo4,)
>
> > hp11
> > bar hp11
>
> William,
> Thanks for the info. This seems like a good start to what I need.
> However, I have two questions for you
> 1) For the section "BEGIN { target = "foo3" }" how do I see this up
> to take info from a for loop or from a file? Do I use an OPEN
> statement?
> 2) The above output does the job on the netgroups and the groups.
> However, I would like to have the entire entry for foobar3 removed
> from the other maps. In these maps the foobar entry is on a line by
> itself. Is this a situation for a case style statement? One for
> netgroups, one for groups, one for printers, etc or can this be
> accomplished in another manner?
> Thanks again for your help.

This version removes blank lines.
The target is passed on the command-line:
awk -f remove.awk target=foo3 datafile

#!awk
# Provide target on command-line before filename.
# E.g., target="foo3"

# Produces array of nonmatching and matching
# substrings. The size of the array will
# always be an odd number. The first and the
# last item will always be nonmatching.
function shatter( s, shards, regexp )
{ gsub( regexp, "\1&\1", s )
return split( s, shards, "\1" )
}

{
size = shatter( $0, array, "[:,()]+" )
if (1 == size)
shatter( $0, array, "[ \t]+" )
out = ""
p = 0
for (i=1; i in array; i++)
if ( array[i] == target )
{ p = i
break
}
if (0 == p)
out = $0
else
for (i=1; i in array; i++)
{
if (i == p-1 )
{ if ( "(," == array[i] )
continue
}
else if ((i == p) || (i == p+1) )
continue
out = out array[i]
}
if ( out !~ /^[ \t]*$/ )
print out
}

Re: Script to modify NIS maps

am 20.09.2007 12:02:25 von William James

On Sep 19, 2:32 pm, William James wrote:
> On Sep 19, 2:18 pm, littlehelph...@gmail.com wrote:
>
> > On Sep 19, 1:47 pm, William James wrote:
>
> > > On Sep 19, 10:55 am, littlehelph...@gmail.com wrote:
>
> > > > On Sep 19, 11:17 am, William James wrote:
>
> > > > > On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
>
> > > > > > On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
> > > > > > > Does anyone know of a script or method to remove users from NIS maps
> > > > > > > via a script. I have been trying to accomplish this via sed, Perl ,
> > > > > > > etc but cannot get it to work. The main issue is each NIS maps has a
> > > > > > > different format. Does anyone know of one Perl command, or any other
> > > > > > > means, that would accomplish this? Thanks.
>
> > > > > > Anyone know of anything that can help me accomplish this?
>
> > > > > Give us a small sample of an NIS map.
>
> > > > William,
> > > > There are numerous maps I would be looking to edit via the tool/
> > > > script. Such maps as netgroup, group,
>
> > > > i.e - I want to remove foo3 from all maps below. You see the format
> > > > and placement will vary on different maps.
>
> > > > group: foousers:*:9999:foo1,foo2,foo4,foo3
> > > > netgroup: (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > > > printers: foo3 hp11
>
> > > # Produces array of nonmatching and matching
> > > # substrings. The size of the array will
> > > # always be an odd number. The first and the
> > > # last item will always be nonmatching.
> > > function shatter( s, shards, regexp )
> > > { gsub( regexp, "\1&\1", s )
> > > return split( s, shards, "\1" )
>
> > > }
>
> > > BEGIN { target = "foo3" }
>
> > > {
> > > size = shatter( $0, array, "[:,()]+" )
> > > if (1 == size)
> > > shatter( $0, array, "[ \t]+" )
> > > out = ""
> > > p = 0
> > > for (i=1; i in array; i++)
> > > if ( array[i] == target )
> > > { p = i
> > > break
> > > }
> > > if (0 == p)
> > > out = $0
> > > else
> > > for (i=1; i in array; i++)
> > > {
> > > if (i == p-1 )
> > > { if ( "(," == array[i] )
> > > continue
> > > }
> > > else if ((i == p) || (i == p+1) )
> > > continue
> > > out = out array[i]
> > > }
> > > print out
>
> > > }
>
> > > ==== input ====
> > > foousers:*:9999:foo1,foo2,foo4,foo3
> > > (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > > (,foo3,)
> > > foo3 hp11
> > > bar foo3 hp11
> > > foo3
> > > ==== output ====
> > > foousers:*:9999:foo1,foo2,foo4,
> > > (,foo1,) (,foo2,) (,foo4,)
>
> > > hp11
> > > bar hp11
>
> > William,
> > Thanks for the info. This seems like a good start to what I need.
> > However, I have two questions for you
> > 1) For the section "BEGIN { target = "foo3" }" how do I see this up
> > to take info from a for loop or from a file? Do I use an OPEN
> > statement?
> > 2) The above output does the job on the netgroups and the groups.
> > However, I would like to have the entire entry for foobar3 removed
> > from the other maps. In these maps the foobar entry is on a line by
> > itself. Is this a situation for a case style statement? One for
> > netgroups, one for groups, one for printers, etc or can this be
> > accomplished in another manner?
> > Thanks again for your help.
>
> This version removes blank lines.
> The target is passed on the command-line:
> awk -f remove.awk target=foo3 datafile
>
> #!awk
> # Provide target on command-line before filename.
> # E.g., target="foo3"
>
> # Produces array of nonmatching and matching
> # substrings. The size of the array will
> # always be an odd number. The first and the
> # last item will always be nonmatching.
> function shatter( s, shards, regexp )
> { gsub( regexp, "\1&\1", s )
> return split( s, shards, "\1" )
>
> }
>
> {
> size = shatter( $0, array, "[:,()]+" )
> if (1 == size)
> shatter( $0, array, "[ \t]+" )
> out = ""
> p = 0
> for (i=1; i in array; i++)
> if ( array[i] == target )
> { p = i
> break
> }
> if (0 == p)
> out = $0
> else
> for (i=1; i in array; i++)
> {
> if (i == p-1 )
> { if ( "(," == array[i] )
> continue
> }
> else if ((i == p) || (i == p+1) )
> continue
> out = out array[i]
> }
> if ( out !~ /^[ \t]*$/ )
> print out}

This way seems simpler.

#!awk
# Provide target on command-line before filename.
# E.g., target="foo3"

BEGIN {
t = "A-Za-z0-9_"
word_chars = "[" t "]+"
sep_chars = "[^" t "]+"
}

# Produces array of nonmatching and matching
# substrings. The size of the array will
# always be an odd number. The first and the
# last item will always be nonmatching.
function shatter( s, shards, regexp )
{ gsub( regexp, "\1&\1", s )
return split( s, shards, "\1" )
}

{
cnt = shatter( $0, ary,
"(^|" sep_chars ")" target "(" sep_chars "|$)" )
if ( cnt > 1 )
{
if ( cnt > 3 )
print "\aMore than 1 in " $0 > "/dev/stderr"
pre = ary[ 1 ]
matched = ary[ 2 ]
post = ""
for (i=3; i in ary; i++)
post = post ary[ i ]
shatter( matched, ary, word_chars )
if ( sub( / *\(,$/, "", ary[1] ) )
sub( /,\)/, "", ary[3] )
else if ( ! sub( /.+/, "", ary[3] ) && ary[1] !~ /:$/ )
ary[1] = ""
out = pre ary[1] ary[3] post
if ( out !~ /^[ \t]*$/ )
print out
}
else
print
}

Re: Script to modify NIS maps

am 20.09.2007 18:08:15 von littlehelphere

On Sep 19, 3:32 pm, William James wrote:
> On Sep 19, 2:18 pm, littlehelph...@gmail.com wrote:
>
>
>
> > On Sep 19, 1:47 pm, William James wrote:
>
> > > On Sep 19, 10:55 am, littlehelph...@gmail.com wrote:
>
> > > > On Sep 19, 11:17 am, William James wrote:
>
> > > > > On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
>
> > > > > > On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
> > > > > > > Does anyone know of a script or method to remove users from NIS maps
> > > > > > > via a script. I have been trying to accomplish this via sed, Perl ,
> > > > > > > etc but cannot get it to work. The main issue is each NIS maps has a
> > > > > > > different format. Does anyone know of one Perl command, or any other
> > > > > > > means, that would accomplish this? Thanks.
>
> > > > > > Anyone know of anything that can help me accomplish this?
>
> > > > > Give us a small sample of an NIS map.
>
> > > > William,
> > > > There are numerous maps I would be looking to edit via the tool/
> > > > script. Such maps as netgroup, group,
>
> > > > i.e - I want to remove foo3 from all maps below. You see the format
> > > > and placement will vary on different maps.
>
> > > > group: foousers:*:9999:foo1,foo2,foo4,foo3
> > > > netgroup: (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > > > printers: foo3 hp11
>
> > > # Produces array of nonmatching and matching
> > > # substrings. The size of the array will
> > > # always be an odd number. The first and the
> > > # last item will always be nonmatching.
> > > function shatter( s, shards, regexp )
> > > { gsub( regexp, "\1&\1", s )
> > > return split( s, shards, "\1" )
>
> > > }
>
> > > BEGIN { target = "foo3" }
>
> > > {
> > > size = shatter( $0, array, "[:,()]+" )
> > > if (1 == size)
> > > shatter( $0, array, "[ \t]+" )
> > > out = ""
> > > p = 0
> > > for (i=1; i in array; i++)
> > > if ( array[i] == target )
> > > { p = i
> > > break
> > > }
> > > if (0 == p)
> > > out = $0
> > > else
> > > for (i=1; i in array; i++)
> > > {
> > > if (i == p-1 )
> > > { if ( "(," == array[i] )
> > > continue
> > > }
> > > else if ((i == p) || (i == p+1) )
> > > continue
> > > out = out array[i]
> > > }
> > > print out
>
> > > }
>
> > > ==== input ====
> > > foousers:*:9999:foo1,foo2,foo4,foo3
> > > (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > > (,foo3,)
> > > foo3 hp11
> > > bar foo3 hp11
> > > foo3
> > > ==== output ====
> > > foousers:*:9999:foo1,foo2,foo4,
> > > (,foo1,) (,foo2,) (,foo4,)
>
> > > hp11
> > > bar hp11
>
> > William,
> > Thanks for the info. This seems like a good start to what I need.
> > However, I have two questions for you
> > 1) For the section "BEGIN { target = "foo3" }" how do I see this up
> > to take info from a for loop or from a file? Do I use an OPEN
> > statement?
> > 2) The above output does the job on the netgroups and the groups.
> > However, I would like to have the entire entry for foobar3 removed
> > from the other maps. In these maps the foobar entry is on a line by
> > itself. Is this a situation for a case style statement? One for
> > netgroups, one for groups, one for printers, etc or can this be
> > accomplished in another manner?
> > Thanks again for your help.
>
> This version removes blank lines.
> The target is passed on the command-line:
> awk -f remove.awk target=foo3 datafile
>
> #!awk
> # Provide target on command-line before filename.
> # E.g., target="foo3"
>
> # Produces array of nonmatching and matching
> # substrings. The size of the array will
> # always be an odd number. The first and the
> # last item will always be nonmatching.
> function shatter( s, shards, regexp )
> { gsub( regexp, "\1&\1", s )
> return split( s, shards, "\1" )
>
> }
>
> {
> size = shatter( $0, array, "[:,()]+" )
> if (1 == size)
> shatter( $0, array, "[ \t]+" )
> out = ""
> p = 0
> for (i=1; i in array; i++)
> if ( array[i] == target )
> { p = i
> break
> }
> if (0 == p)
> out = $0
> else
> for (i=1; i in array; i++)
> {
> if (i == p-1 )
> { if ( "(," == array[i] )
> continue
> }
> else if ((i == p) || (i == p+1) )
> continue
> out = out array[i]
> }
> if ( out !~ /^[ \t]*$/ )
> print out
>
> }

William,
I ran the script as decribed above but I keep getting an error at
the initial function - not sure why
Steps taken - copied the script as remove.aw and made it executable.
I then ran
awk -f remove.awk target=foo3 datafile
and received he following message
awk: syntax error near line 9
awk: bailing out near line 9

Re: Script to modify NIS maps

am 20.09.2007 18:29:11 von William James

On Sep 20, 11:08 am, littlehelph...@gmail.com wrote:
> On Sep 19, 3:32 pm, William James wrote:
>
> > On Sep 19, 2:18 pm, littlehelph...@gmail.com wrote:
>
> > > On Sep 19, 1:47 pm, William James wrote:
>
> > > > On Sep 19, 10:55 am, littlehelph...@gmail.com wrote:
>
> > > > > On Sep 19, 11:17 am, William James wrote:
>
> > > > > > On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
>
> > > > > > > On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
> > > > > > > > Does anyone know of a script or method to remove users from NIS maps
> > > > > > > > via a script. I have been trying to accomplish this via sed, Perl ,
> > > > > > > > etc but cannot get it to work. The main issue is each NIS maps has a
> > > > > > > > different format. Does anyone know of one Perl command, or any other
> > > > > > > > means, that would accomplish this? Thanks.
>
> > > > > > > Anyone know of anything that can help me accomplish this?
>
> > > > > > Give us a small sample of an NIS map.
>
> > > > > William,
> > > > > There are numerous maps I would be looking to edit via the tool/
> > > > > script. Such maps as netgroup, group,
>
> > > > > i.e - I want to remove foo3 from all maps below. You see the format
> > > > > and placement will vary on different maps.
>
> > > > > group: foousers:*:9999:foo1,foo2,foo4,foo3
> > > > > netgroup: (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > > > > printers: foo3 hp11
>
> > > > # Produces array of nonmatching and matching
> > > > # substrings. The size of the array will
> > > > # always be an odd number. The first and the
> > > > # last item will always be nonmatching.
> > > > function shatter( s, shards, regexp )
> > > > { gsub( regexp, "\1&\1", s )
> > > > return split( s, shards, "\1" )
>
> > > > }
>
> > > > BEGIN { target = "foo3" }
>
> > > > {
> > > > size = shatter( $0, array, "[:,()]+" )
> > > > if (1 == size)
> > > > shatter( $0, array, "[ \t]+" )
> > > > out = ""
> > > > p = 0
> > > > for (i=1; i in array; i++)
> > > > if ( array[i] == target )
> > > > { p = i
> > > > break
> > > > }
> > > > if (0 == p)
> > > > out = $0
> > > > else
> > > > for (i=1; i in array; i++)
> > > > {
> > > > if (i == p-1 )
> > > > { if ( "(," == array[i] )
> > > > continue
> > > > }
> > > > else if ((i == p) || (i == p+1) )
> > > > continue
> > > > out = out array[i]
> > > > }
> > > > print out
>
> > > > }
>
> > > > ==== input ====
> > > > foousers:*:9999:foo1,foo2,foo4,foo3
> > > > (,foo1,) (,foo2,) (,foo3,) (,foo4,)
> > > > (,foo3,)
> > > > foo3 hp11
> > > > bar foo3 hp11
> > > > foo3
> > > > ==== output ====
> > > > foousers:*:9999:foo1,foo2,foo4,
> > > > (,foo1,) (,foo2,) (,foo4,)
>
> > > > hp11
> > > > bar hp11
>
> > > William,
> > > Thanks for the info. This seems like a good start to what I need.
> > > However, I have two questions for you
> > > 1) For the section "BEGIN { target = "foo3" }" how do I see this up
> > > to take info from a for loop or from a file? Do I use an OPEN
> > > statement?
> > > 2) The above output does the job on the netgroups and the groups.
> > > However, I would like to have the entire entry for foobar3 removed
> > > from the other maps. In these maps the foobar entry is on a line by
> > > itself. Is this a situation for a case style statement? One for
> > > netgroups, one for groups, one for printers, etc or can this be
> > > accomplished in another manner?
> > > Thanks again for your help.
>
> > This version removes blank lines.
> > The target is passed on the command-line:
> > awk -f remove.awk target=foo3 datafile
>
> > #!awk
> > # Provide target on command-line before filename.
> > # E.g., target="foo3"
>
> > # Produces array of nonmatching and matching
> > # substrings. The size of the array will
> > # always be an odd number. The first and the
> > # last item will always be nonmatching.
> > function shatter( s, shards, regexp )
> > { gsub( regexp, "\1&\1", s )
> > return split( s, shards, "\1" )
>
> > }
>
> > {
> > size = shatter( $0, array, "[:,()]+" )
> > if (1 == size)
> > shatter( $0, array, "[ \t]+" )
> > out = ""
> > p = 0
> > for (i=1; i in array; i++)
> > if ( array[i] == target )
> > { p = i
> > break
> > }
> > if (0 == p)
> > out = $0
> > else
> > for (i=1; i in array; i++)
> > {
> > if (i == p-1 )
> > { if ( "(," == array[i] )
> > continue
> > }
> > else if ((i == p) || (i == p+1) )
> > continue
> > out = out array[i]
> > }
> > if ( out !~ /^[ \t]*$/ )
> > print out
>
> > }
>
> William,
> I ran the script as decribed above but I keep getting an error at
> the initial function - not sure why
> Steps taken - copied the script as remove.aw and made it executable.
> I then ran
> awk -f remove.awk target=foo3 datafile
> and received he following message
> awk: syntax error near line 9
> awk: bailing out near line 9


If you have nawk, use it instead of awk because on some
systems awk is very old and lacks many useful features.
Under Solaris, use /usr/xpg4/bin/awk.