Operating System - Linux
1753873 Members
7144 Online
108809 Solutions
New Discussion юеВ

awk operand !~ not working?

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

awk operand !~ not working?

Hello all,

I cant get the !~ operand to work - my syntax:

awk '/GIO-ES-UAM\:/ {if ($0 !~ /^\+@GIO/) print "+@GIO-ES-UAM:"}' /etc/passwd

so I am looking for the string GIO-ES-UAM in /etc/passwd and if not then print etc ...

I get no output however if I add +@GIO-ES-UAM: into /etc/passwd and run:

awk '/GIO-ES-UAM\:/ {if ($0 ~ /^\+@GIO/) print "+@GIO-ES-UAM:"}' /etc/passwd

this works?

can someone see where my mistake lies?

Thanks

Chris.
hello
12 REPLIES 12
Rasheed Tamton
Honored Contributor
Solution

Re: awk operand !~ not working?

Hi Chris,

If I get your question correctly:

awk -F: '$0 ~ /\+@GIO-ES-UAM/ {print "+@GIO-ES-UAM:"}' passwd

Otherwise, please explain in detail.

Regards,
Rasheed Tamton.
lawrenzo_1
Super Advisor

Re: awk operand !~ not working?

ok my question is

I want to search for the string "+@GIO-ES-UAM:" in /etc/passwd - if the string does not exist in /etc/passwd I want awk to print the string.

I have used !~ (logical not) in the if statement but it wont print.

can you see where i am going wrong with my syntax?

Thanks
hello
Rasheed Tamton
Honored Contributor

Re: awk operand !~ not working?

awk -F: '$0 !~ /\+@GIO-ES-UAM/ {print "+@GIO-ES-UAM:"}' passwd

But this will print the string for all the lines it gets as input from the passwd file.

Regards.
Regards.
lawrenzo_1
Super Advisor

Re: awk operand !~ not working?

I run your example

awk '$0 ~ /\+@GIO-ES-UAM/ {print "+@GIO-ES-UAM:"}' /etc/passwd

and there is no output to the command .....

I run awk '$0 !~ /\+@GIO-ES-UAM/ {print "+@GIO-ES-UAM:"}' /etc/passwd

and I get a print "+@GIO-ES-UAM:" for each line in etc passwd.

root@tstestcl.eu.unilever.com # awk '$0 !~ /\+@GIO-ES-UAM/ {print "+@GIO-ES-UAM:"}' /etc/passwd
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:
+@GIO-ES-UAM:


I want this to be displayed if there is no entry for "+@GIO-ES-UAM:" in /etc/passwd.

thanks

hello
Dennis Handly
Acclaimed Contributor

Re: awk operand !~ not working?

>I can't get the !~ operand to work - my syntax:

awk '/GIO-ES-UAM\:/ {if ($0 !~ /^\+@GIO/) print "+@GIO-ES-UAM:"}' /etc/passwd

This seems to search for "GIO-ES-UAM:". (Why are you quoting a ":"?)
Then it prints lines that don't start with "+@GIO"

>so I am looking for the string GIO-ES-UAM in /etc/passwd and if not then print etc ...

Why not pipe two greps to do the two searches?
$ grep "GIO-ES-UAM:" /etc/passwd | grep -v "+@GIO"

>I get no output however if I add +@GIO-ES-UAM: into /etc/passwd and run:
awk '/GIO-ES-UAM\:/ {if ($0 ~ /^\+@GIO/) print "+@GIO-ES-UAM:"}' /etc/passwd

This prints if you match both.

>I want to search for the string "+@GIO-ES-UAM:" in /etc/passwd - if the string does not exist in /etc/passwd I want awk to print the string.

You want to search the WHOLE file and if doesn't exist print a string? Why not just use grep?
fgrep -q "GIO-ES-UAM:" /etc/passwd
if [ $? -ne 0 ]; then
echo "+@GIO-ES-UAM:"
fi

>can you see where i am going wrong with my syntax?

We either need some more details or an example file.

>I want this to be displayed if there is no entry for "+@GIO-ES-UAM:" in /etc/passwd.

If you want this in awk, you must use flag programming. You set the flag if you find it and print it at the end:
awk '
BEGIN { found = 0 }
/GIO-ES-UAM:/ { found = 1 }
END { if (!found) print "+@GIO-ES-UAM:" }' /etc/passwd
Dennis Handly
Acclaimed Contributor

Re: awk operand !~ not working?

You could optimize this and stop when you find it:
awk '
/GIO-ES-UAM:/ { exit }
END { print "+@GIO-ES-UAM:" }' /etc/passwd
lawrenzo_1
Super Advisor

Re: awk operand !~ not working?

ok i can see where your going with this ....

it doesnt work because my logic is out of sync?

I am searching for any occurance of GIO-ES-UAM in /etc/passwd with '/GIO-ES-UAM/.

if it doesnt exist I run if ($0 !~ /\^+@GIO-ES-UAM:/) print etc etc ....

as this is not logical ....

I'll use grep - and thanks
hello
Dennis Handly
Acclaimed Contributor

Re: awk operand !~ not working?

>it doesn't work because my logic is out of sync?

Basically awk (and sed) work on each line of the input file. You want something that works on all lines and then returns a status or message. You can do that with END {}.
James R. Ferguson
Acclaimed Contributor

Re: awk operand !~ not working?

Hi Chris:

You are saying that you want to match the character sequence "GIO-ES-UAM:" *anywhere* in a record, *and* then using the matched record, if the *beginning* character sequence is *not* "+@GIO", then print.

So you are looking for NIS entries. I suspect that you are interested in one whose GECOS field (#5) contains "GIO-ES-UAM". If that's the case, you could specify '-F:' as the 'awk' field delimiter and match on $5 and $1:

# awk -F: '{if ($1~/\+@GIO/ && $5~/GIO-ES-UAM/) print "+@GIO-ES-UAM:"}' /etc/passwd

You said:

> I get no output however if I add +@GIO-ES-UAM: into /etc/passwd and run [my command[.

You didn't add the extra colon (which would be an extra field delimiter) did you?

Regards!

...JRF...