1

I'm using awk to write a bash script that outputs the count of error codes ranging from 400 - 500 that appear in a text file called output.txt

awk '($9 >= 400)' output.txt | awk '{print $9}' | sort | uniq -c

The output of the above is:

  12 400
  11 401
  55 403
  91 404
  41 500

How do I add the first column together using bash so that in the example above, I get 210 instead of the above output... (12 + 11 + 55 + 91 + 41 = 210)

And if I wanted to input in a file via command line argument instead of output.txt how should I edit the script? I know that you use '$1' and '$2' to access command line arguments, but in this case how would it work considering I'm already using $9 in with awk

2
  • Please add sample input (no descriptions, no images, no links) and your desired output for that sample input to your question (no comment). Commented Mar 30, 2020 at 16:47
  • When you say "concatenate" ITYM "add". When you concatenate 3 and 6 you get 36, when you add 3 and 6 you get 9. Commented Mar 30, 2020 at 17:47

2 Answers 2

3

To fix your problem immediate since you are using already 1 command, could try as(though I am pretty sure if you could share sample of your input we could probably do it in a single command too):

your_command | awk '{sum+=$1} END{print sum}'

Also in OP's command part awk '($9 >= 400)' output.txt | awk '{print $9}' could be shorten to awk '($9 >= 400){print $9}' output.txt

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

4 Comments

And the whole pipeline can be: awk '$9 > 400 {sum++} END {print sum}' output.txt
@glennjackman, Sorry sir I apologies, I didn't quite understand it. May I request you to please do edit my answer for better understand please.
hi @RavinderSingh13 how would you update the script so that it can will read from command line argument instead of output.txt. So i want to just call it like so./statuscode.sh output.txt
@sgeza, change in your script like, var="$1" and where in your command you are mentioning output.txt now put "$var" in its place. Lemme know how it goes.
1

To get the count of error codes ranging from 400 - 500 that appear in a text file called output.txt assuming each error code is in $9 is:

awk '($9 >= 400) && ($9 <= 500) && !seen[$9]++{cnt++} END{print cnt+0}' output.txt

To get the count of **lines containing** error codes ranging from 400 - 500 that appear in a text file called output.txt assuming each error code is in $9 is:

awk '($9 >= 400) && ($9 <= 500){cnt++} END{print cnt+0}' output.txt

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.