|
Home > Archive > Unix Shell > September 2007 > Script to modify NIS maps
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Script to modify NIS maps
|
|
| littlehelphere@gmail.com 2007-09-18, 1:27 pm |
| 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.
| |
| littlehelphere@gmail.com 2007-09-19, 1:26 pm |
| 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?
| |
| William James 2007-09-19, 1:26 pm |
| On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
> On Sep 18, 9:50 am, littlehelph...@gmail.com wrote:
>
>
> Anyone know of anything that can help me accomplish this?
Give us a small sample of an NIS map.
| |
| littlehelphere@gmail.com 2007-09-19, 1:26 pm |
| On Sep 19, 11:17 am, William James <w_a_x_...@yahoo.com> wrote:
> On Sep 19, 9:38 am, littlehelph...@gmail.com wrote:
>
>
>
>
> 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
| |
| William James 2007-09-19, 1:26 pm |
| On Sep 19, 10:55 am, littlehelph...@gmail.com wrote:
> On Sep 19, 11:17 am, William James <w_a_x_...@yahoo.com> wrote:
>
>
>
>
>
>
> 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
| |
| littlehelphere@gmail.com 2007-09-19, 7:19 pm |
| On Sep 19, 1:47 pm, William James <w_a_x_...@yahoo.com> wrote:
> On Sep 19, 10:55 am, littlehelph...@gmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
> # 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.
| |
| William James 2007-09-19, 7:19 pm |
| On Sep 19, 2:18 pm, littlehelph...@gmail.com wrote:
> On Sep 19, 1:47 pm, William James <w_a_x_...@yahoo.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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 James 2007-09-20, 7:30 am |
| On Sep 19, 2:32 pm, William James <w_a_x_...@yahoo.com> wrote:
> On Sep 19, 2:18 pm, littlehelph...@gmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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
}
| |
| littlehelphere@gmail.com 2007-09-20, 1:30 pm |
| On Sep 19, 3:32 pm, William James <w_a_x_...@yahoo.com> wrote:
> On Sep 19, 2:18 pm, littlehelph...@gmail.com wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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
| |
| William James 2007-09-20, 1:30 pm |
| On Sep 20, 11:08 am, littlehelph...@gmail.com wrote:
> On Sep 19, 3:32 pm, William James <w_a_x_...@yahoo.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> 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.
|
|
|
|
|