Search code examples
shellawkcut

how to combine cut command with arithmetic comparison


Good morning all,

I have a file called test.txt

audit.log.20140612:6/11/14 6:27:33 AM;SUCCESS;998ms;10.62.172.52;52750;Issue
audit.log.20140612:6/11/14 6:28:25 AM;SUCCESS;580ms;10.62.172.52;52750;Issue;
audit.log.20140612:6/11/14 6:28:25 AM;SUCCESS;500ms;10.62.172.52;52750;Issue;
audit.log.20140612:6/11/14 6:28:25 AM;SUCCESS;58ms;10.62.172.52;52750;Issue;

Now I wanted to extract all the line which have more than 100ms(milli seconds).

I achieved till getting 3rd field form following command,

cut -d ';' -f 3 test.txt

Now I want to pipe this results to some shell command so that i can compare the results. where the time is > 100ms

Finally my output should be,

audit.log.20140612:6/11/14 6:27:33 AM;SUCCESS;998ms;10.62.172.52;52750;Issue
audit.log.20140612:6/11/14 6:28:25 AM;SUCCESS;580ms;10.62.172.52;52750;Issue;
audit.log.20140612:6/11/14 6:28:25 AM;SUCCESS;500ms;10.62.172.52;52750;Issue;

Solution

  • With awk:

    awk -F';' '$3+0 > 100' yourfile
    

    With bash:

    #!/bin/bash                                      
    while read line; do 
            fields=(${line//;/ });
            t=${fields[4]}; 
            if [ ${t%ms} -gt 100 ]; 
            then 
                    echo "$line"; 
            fi; 
    done< yourfile