Search code examples
awksed

i wish to compare 2 numeric values in a column seperate by slash


If i get an output like below from another command or a file

uniquename1: count 2/3
uniquename2: count 3/3
uniquename3: count 1/1
uniquename4: count 3/1
uniquename5: count 0/0
uniquename6: count 1/1
uniquename7: count 1/3
uniquename8: count 2/2

How can i compare the numbers surrounding the / on each line.

Where if they match then return a result saying '$uniquename working'

Where if they the numbers do not match then return a result saying '$uniquename warning'

Where if they the first number is 0 then return a result saying '$uniquename broken'

uniquename1: warning
uniquename2: working
uniquename3: working
uniquename4: warning
uniquename5: broken
uniquename6: working
uniquename7: warning
uniquename8: working

My knowledge is limited and current attempts see me replacing the / then comparing columns 3 and 4 and then printing the name that appears before : but its a mess and bad

Another way to phrase my need:

The number before the / is actual count

The number after / is expected count

If actual = 0 then its bad

If actual = expected then its good

If actual does not equal 0 or does not equal expected then its not good but not broken


Solution

  • This seems to work:

    % sed 's%count 0/0%broken%;/ count \(.*\)\/\1$/!s%count .*%warning%;s%count \(.*\)/\1$%working%' file.txt
    

    The first section says to swap "count 0/0" with broken (if possible). Second part, after the ";", says if the thing between "count " and "/" does NOT match what comes after "/" then sub in warning. The third part says sub in "working" if they do match.

    ❯ cat /tmp/file.txt
    uniquename1: count 2/3
    uniquename2: count 3/3
    uniquename3: count 1/1
    uniquename4: count 3/1
    uniquename5: count 0/0
    uniquename6: count 1/1
    uniquename7: count 1/3
    uniquename8: count 2/2
    ❯ sed 's%count 0/0%broken%;/ count \(.*\)\/\1$/!s%count .*%warning%;s%count \(.\)/\1$%working%' /tmp/file.txt
    uniquename1: warning
    uniquename2: working
    uniquename3: working
    uniquename4: warning
    uniquename5: broken
    uniquename6: working
    uniquename7: warning
    uniquename8: working
    

    I think the sample output does not match my understanding of the question.