Tag: awk

Using AWK to display the min the max and the average of a list

awk is a extremely powerful tool !
i’m always surprised how much you can do with it.

My latest finding is pretty amazing.

Lets say you have a list of numbers , and you want to figure out the bigger number , the lower number , and the average of all the numbers in the list.

you can use the following awk code to print minimum, maximum and average.

The output is going to  be :
avg 32.93 | max 100.1 | min 1.5

you can also use this code with decimal numbers

 

Use AWK to display x-th and y-th lines after a regex match

In a script you might be interested to keep 2 or more lines after a regex match ,
the line that interest you are not always going to be right after the match ,
the first one might be X lines after the match and the second one Y lines afters the match

in this example , i’m interested in keeping the lines with the price and the number in stock
i’m going to use awk to search for item desc , and display the 4th line after the match, and 6th line.

Extract values separated by character ( : , . – )

In a bash script if you find yourself with values presented that way

you might want to keep only the second or third of this values that are separated by the : character

like always , in bash there is a lot of ways to accomplish that , but i like to use a one that use the AWK utility because it’s very easy to read and understand.

my list of value is contained in a var name $var let’s extract the third value which is 18.5

Will return the value 18.5

but i can do some math operation in the utility, like :

this will return 326.5 the result of 345 – 18.5

awk is a fast and powerful text editor , there is a lot things you can do with it.