awk match question

awk match question

am 19.11.2007 19:42:06 von Ben Jackson

Hi,

I'm trying to remove lines from auto.home ona a Red Hat AS4 server,
based on a list of account names that are to be closed.

My loop condition together with awk/match is not working well, could you
please tell me what I'm doing wrong in passing variables?

===============

for user in 'cat "removable"' ; do
awk 'match($0,"$user") == 0 {print $0} auto.fake>aotu.fake.new
done

===============

Many thanks, Ben

Re: awk match question

am 19.11.2007 20:19:20 von Janis Papanagnou

Ben wrote:
> Hi,
>
> I'm trying to remove lines from auto.home ona a Red Hat AS4 server,
> based on a list of account names that are to be closed.
>
> My loop condition together with awk/match is not working well, could you
> please tell me what I'm doing wrong in passing variables?
>
> ===============
>
> for user in 'cat "removable"' ; do

I suppose you wanted to use backticks instead of single quotes, or
(assuming removable is the name of the file containing users)...

for user in $(cat removable) ; do

> awk 'match($0,"$user") == 0 {print $0} auto.fake>aotu.fake.new

There's a closing single quote missing and you are tryoing to use a
shell variable in awk context.


What you maybe wanted to do instead of the above... [untested code]

awk 'NR==FNR {u[$1];next} !($i in u)' removable auto.fake >auto.fake.new

....where you have to replace i by the column number of the field in
file auto.fake that contains the user name.

Janis

> done
>
> ===============
>
> Many thanks, Ben

Re: awk match question

am 19.11.2007 20:48:57 von Ben Jackson

Janis Papanagnou wrote:

> Ben wrote:
>
>> Hi,
>>
>> I'm trying to remove lines from auto.home ona a Red Hat AS4 server,
>> based on a list of account names that are to be closed.
>>
>> My loop condition together with awk/match is not working well, could
>> you please tell me what I'm doing wrong in passing variables?
>>
>> ===============
>>
>> for user in 'cat "removable"' ; do
>
>
> I suppose you wanted to use backticks instead of single quotes, or
> (assuming removable is the name of the file containing users)...
>
> for user in $(cat removable) ; do
>
>> awk 'match($0,"$user") == 0 {print $0} auto.fake>aotu.fake.new
>
>
> There's a closing single quote missing and you are tryoing to use a
> shell variable in awk context.
>
>
> What you maybe wanted to do instead of the above... [untested code]
>
> awk 'NR==FNR {u[$1];next} !($i in u)' removable auto.fake >auto.fake.new
>
> ...where you have to replace i by the column number of the field in
> file auto.fake that contains the user name.
>
> Janis
>
>> done
>>
>> ===============
>>
>> Many thanks, Ben

It seems to work well for the small test file I have...manny thanks!

As I undestand your code, NR==FNR {u[$1];next} takes care of the loop
and !($i in u)' is the condition, am I right?

Thanks again, Ben

Re: awk match question

am 19.11.2007 21:32:36 von Janis Papanagnou

Ben wrote:
> Janis Papanagnou wrote:
>
>> Ben wrote:
>>
>>> Hi,
>>>
>>> I'm trying to remove lines from auto.home ona a Red Hat AS4 server,
>>> based on a list of account names that are to be closed.
>>>
>>> My loop condition together with awk/match is not working well, could
>>> you please tell me what I'm doing wrong in passing variables?
>>>
>>> ===============
>>>
>>> for user in 'cat "removable"' ; do
>>
>>
>>
>> I suppose you wanted to use backticks instead of single quotes, or
>> (assuming removable is the name of the file containing users)...
>>
>> for user in $(cat removable) ; do
>>
>>> awk 'match($0,"$user") == 0 {print $0} auto.fake>aotu.fake.new
>>
>>
>>
>> There's a closing single quote missing and you are tryoing to use a
>> shell variable in awk context.
>>
>>
>> What you maybe wanted to do instead of the above... [untested code]
>>
>> awk 'NR==FNR {u[$1];next} !($i in u)' removable auto.fake >auto.fake.new
>>
>> ...where you have to replace i by the column number of the field in
>> file auto.fake that contains the user name.
>>
>> Janis
>>
>>> done
>>>
>>> ===============
>>>
>>> Many thanks, Ben
>
>
> It seems to work well for the small test file I have...manny thanks!
>
> As I undestand your code, NR==FNR {u[$1];next} takes care of the loop

No. That NR==FNR is the condition valid while processing the first file
where the entries (of file "removable") will simply be stored.

> and !($i in u)' is the condition, am I right?

Because of the above 'next' that condition will be checked for NR!=FNR
(i.e. for all subsequent files). If the username is not in the stored
set u then the default action (to print the line) takes place, other
lines (where the user name is in the stored set u) are not printed.

Janis

>
> Thanks again, Ben