Results 1 to 7 of 7

Thread: Awk and comparison operators

  1. #1
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,509
    Distro
    Lubuntu 14.04 Trusty Tahr

    Awk and comparison operators

    I'm trying to understand how comparison operators for awk work. I made a file, awkcomp.txt:
    Code:
    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:
    1. awk -F: '$2 == "longword" { print }' awkcomp.txt prints only line 5 because in that line $1 is exactly equal to "longword".
    2. awk -F: '$2 ~ "longword" { print }' awkcomp.txt seems less stringent and prints lines 5, 6, and 7 because they all contain "longword" in $1.
    3. awk -F: '$2 != "longword" { print }' awkcomp.txt prints all lines except line 5.
    4. awk -F: '$2 < "longword" { print }' awkcomp.txt prints lines 1, 4, 6, 8, and 10.
    5. 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.
    Last edited by vasa1; September 27th, 2013 at 04:38 AM. Reason: included serial numbers and changed $1 to $2 in commands
    de gustibus et coloribus non est disputandum -- Wiktionary

  2. #2
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,247
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Awk and comparison operators

    alphanumeric order (think dictionary)

    Code:
    $ 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
    Last edited by Vaphell; September 26th, 2013 at 07:25 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  3. #3
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,509
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Awk and comparison operators

    Quote Originally Posted by Vaphell View Post
    alphanumeric order (think dictionary)
    ...
    Thanks! It didn't seem clear at first but after reading your post, I sorted awkcomp.txt to get:
    Code:
    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]
    Last edited by vasa1; September 27th, 2013 at 04:50 AM. Reason: edits made to reflect change in first post
    de gustibus et coloribus non est disputandum -- Wiktionary

  4. #4
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,509
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Awk and comparison operators

    I also changed Vaphell's command:
    Code:
    echo $'a\naa\nac\nb\ncc' | awk '$1<"ab" { print $1 " < ab"; }; $1>"ab" { print $1 " > ab"; }'
    to
    Code:
    awk -F: '$2<"longword" { print $2 " < longword"; }; $2>"longword" { print $2 " > longword"; }' awkcomp.txt
    and got
    Code:
    dnsmasq < longword
    ntp > longword
    whoopsie > longword
    lightdm < longword
    alongword < longword
    longworda > longword
    longwo < longword
    ngword > longword
    avahi < longword
    de gustibus et coloribus non est disputandum -- Wiktionary

  5. #5
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,509
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Awk and comparison operators

    Quote Originally Posted by Vaphell View Post
    ...
    Code:
    ...
    $ 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 ";":
    Code:
    [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,
    Code:
    awk -F: '$2<"longword" { print $2 " < longword"; }; $2>"longword" { print $2 " > longword"; }' awkcomp.txt
    and
    Code:
    awk -F: '$2<"longword" { print $2 " < longword" } $2>"longword" { print $2 " > longword" }' awkcomp.txt
    both give:
    Code:
    dnsmasq < longword
    ntp > longword
    whoopsie > longword
    lightdm < longword
    alongword < longword
    longworda > longword
    longwo < longword
    ngword > longword
    avahi < longword
    de gustibus et coloribus non est disputandum -- Wiktionary

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,247
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Awk and comparison operators

    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.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  7. #7
    Join Date
    Dec 2007
    Location
    Bombay
    Beans
    5,509
    Distro
    Lubuntu 14.04 Trusty Tahr

    Re: Awk and comparison operators

    Quote Originally Posted by Vaphell View Post
    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!
    de gustibus et coloribus non est disputandum -- Wiktionary

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •