PDA

View Full Version : [SOLVED] Awk and comparison operators



vasa1
September 26th, 2013, 06:30 PM
I'm trying to understand how comparison operators for awk work. I made a file, awkcomp.txt:

01:dnsmasq:x:104:65534:dnsmasq,,,:/var/lib/misc:/bin/false
02:ntp:x:105:109::/home/ntp:/bin/false
03:whoopsie:x:106:110::/nonexistent:/bin/false
04:lightdm:x:107:115:Light Display Manager:/var/lib/lightdm:/bin/false
05:longword:x:1000:1000:longword,,,:/home/longword:/bin/bash
06:alongword:x:1000:1000:longword,,,:/home/longword:/bin/bash
07:longworda:x:1000:1000:longword,,,:/home/longword:/bin/bash
08:longwo:x:1000:1000:longword,,,:/home/longword:/bin/bash
09:ngword:x:1000:1000:longword,,,:/home/longword:/bin/bash
10:avahi:x:108:118:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
Then, I ran the following commands:


awk -F: '$2 == "longword" { print }' awkcomp.txt prints only line 5 because in that line $1 is exactly equal to "longword".
awk -F: '$2 ~ "longword" { print }' awkcomp.txt seems less stringent and prints lines 5, 6, and 7 because they all contain "longword" in $1.
awk -F: '$2 != "longword" { print }' awkcomp.txt prints all lines except line 5.
awk -F: '$2 < "longword" { print }' awkcomp.txt prints lines 1, 4, 6, 8, and 10.
awk -F: '$2 > "longword" { print }' awkcomp.txt prints lines 2, 3, 7, and 9.

While commands 1, 2, and 3 give understandable results, I don't understand what "<" in command 4 and ">" in command 5 do other than not printing any common lines between them. I couldn't make out any pattern as to the lines printed with these two commands.

Vaphell
September 26th, 2013, 07:19 PM
alphanumeric order (think dictionary)


$ echo $'a\naa\nac\nb\ncc'
a
aa
ac
b
cc
$ echo $'a\naa\nac\nb\ncc' | awk '$1<"ab" { print $1 " < ab"; }; $1>"ab" { print $1 " > ab"; }'
a < ab
aa < ab
ac > ab
b > ab
cc > ab

vasa1
September 26th, 2013, 07:57 PM
alphanumeric order (think dictionary)
...
Thanks! It didn't seem clear at first but after reading your post, I sorted awkcomp.txt to get:

06:alongword:x:1000:1000:longword,,,:/home/longword:/bin/bash
10:avahi:x:108:118:Avahi mDNS daemon,,,:/var/run/avahi-daemon:/bin/false
01:dnsmasq:x:104:65534:dnsmasq,,,:/var/lib/misc:/bin/false
04:lightdm:x:107:115:Light Display Manager:/var/lib/lightdm:/bin/false
08:longwo:x:1000:1000:longword,,,:/home/longword:/bin/bash
05:longword:x:1000:1000:longword,,,:/home/longword:/bin/bash
07:longworda:x:1000:1000:longword,,,:/home/longword:/bin/bash
09:ngword:x:1000:1000:longword,,,:/home/longword:/bin/bash
02:ntp:x:105:109::/home/ntp:/bin/false
03:whoopsie:x:106:110::/nonexistent:/bin/false

and those above "longword" were in the "<" group and those below "longword" were in the ">" group.

Marked as [Solved]

vasa1
September 27th, 2013, 07:35 AM
I also changed Vaphell's command:

echo $'a\naa\nac\nb\ncc' | awk '$1<"ab" { print $1 " < ab"; }; $1>"ab" { print $1 " > ab"; }'to

awk -F: '$2<"longword" { print $2 " < longword"; }; $2>"longword" { print $2 " > longword"; }' awkcomp.txtand got

dnsmasq < longword
ntp > longword
whoopsie > longword
lightdm < longword
alongword < longword
longworda > longword
longwo < longword
ngword > longword
avahi < longword

vasa1
September 27th, 2013, 12:35 PM
...

...
$ echo $'a\naa\nac\nb\ncc' | awk '$1<"ab" { print $1 " < ab"; }; $1>"ab" { print $1 " > ab"; }'
a < ab
aa < ab
ac > ab
b > ab
cc > ab

I have a follow-up question ... What is the use of ";" in the code? I get the same result after removing all the instances of ";":
[04:52 PM] ~ $ echo $'a\naa\nac\nb\ncc' | awk '$1<"ab" { print $1 " < ab" } $1>"ab" { print $1 " > ab" }'
a < ab
aa < ab
ac > ab
b > ab
cc > ab
[04:53 PM] ~ $


Similarly,
awk -F: '$2<"longword" { print $2 " < longword"; }; $2>"longword" { print $2 " > longword"; }' awkcomp.txtand
awk -F: '$2<"longword" { print $2 " < longword" } $2>"longword" { print $2 " > longword" }' awkcomp.txtboth give:
dnsmasq < longword
ntp > longword
whoopsie > longword
lightdm < longword
alongword < longword
longworda > longword
longwo < longword
ngword > longword
avahi < longword

Vaphell
September 27th, 2013, 04:29 PM
they are not necessary but i like using them in oneliners so i don't have to think much where one thing ends and another starts.

vasa1
September 27th, 2013, 05:25 PM
they are not necessary but i like using them in oneliners so i don't have to think much where one thing ends and another starts.
Okay and thanks!