- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- awk operand !~ not working?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 01:47 AM
тАО05-14-2008 01:47 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 02:40 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 02:52 AM
тАО05-14-2008 02:52 AM
Re: awk operand !~ not working?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 03:36 AM
тАО05-14-2008 03:36 AM
Re: awk operand !~ not working?
But this will print the string for all the lines it gets as input from the passwd file.
Regards.
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 03:37 AM
тАО05-14-2008 03:37 AM
Re: awk operand !~ not working?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 03:41 AM
тАО05-14-2008 03:41 AM
Re: awk operand !~ not working?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 03:48 AM
тАО05-14-2008 03:48 AM
Re: awk operand !~ not working?
awk '
/GIO-ES-UAM:/ { exit }
END { print "+@GIO-ES-UAM:" }' /etc/passwd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 03:57 AM
тАО05-14-2008 03:57 AM
Re: awk operand !~ not working?
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 04:06 AM
тАО05-14-2008 04:06 AM
Re: awk operand !~ not working?
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 {}.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-14-2008 04:18 AM
тАО05-14-2008 04:18 AM
Re: awk operand !~ not working?
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...