0

I have a shell script that gives me a text file output in following format: OUTPUT.TXT

FirmA
58

FirmB
58

FirmC
58

FirmD
58

FirmE
58

This output is good or a YES that my job completed as expected since the value for all of the firms is 58.

So I used to take count of '58' in this text file to automatically tell in a RESULT job that everything worked out well.

Now there seems to be a bug due to which for sometimes the output comes like below: OUTPUT.TXT

FirmA
58

FirmB
58

FirmC
61

FirmD
58

FirmE
61

which is impacting my count(since only 3 counts of 58 instead of expected 5) and hence my RESULT job states that it FAILED or a NO.

But actually the job has worked fine as long as the value stays within 58 to 61 for each firm.

So how can I ensure that in case the count is >=58 and <=61 for these five firms, than it has worked as expected ?

my simple one liner to check count in OUTPUT.TXT file

grep -cow 58 "OUTPUT.TXT"

1 Answer 1

1

Try Awk for simple jobs like this. You can learn enough in an hour to solve these problems yourself easily.

awk '(NR % 3 == 2) && ($1 < 58 || $1 > 61)' OUTPUT.TXT

This checks every third line, starting from the second, and prints any which are not in the range 58 to 61.

It would not be hard to extend the script to remember the string from the previous line. In fact, let's do that.

awk '(NR % 3 == 1) { firm = $0; next }
    (NR % 3 == 2) && ($1 < 58 || $1 > 61) { print NR ":" firm, $0 }' OUTPUT.TXT

You might also want to check how many you get of each. But let's just make a separate script for that.

awk '(NR % 3 == 2) { ++a[$1] }
   END { for (k in a) print k, a[k] }' OUTPUT.TXT

The Stack Overflow awk tag info page has links to learning materials etc.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.