Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AWK output format

Status
Not open for further replies.

arrals

Technical User
May 4, 2012
5
IT
Hi all,

i have written this script:


Code:
awk -F';' '
BEGIN {
printf "\n"
printf "\n"
printf "\n"
printf "----------------------------------------------\n"
print " For test "
printf "----------------------------------------------\n"
test_200 = 0
test_300 = 0
test_500 = 0
test_1000 = 0
test_nok = 0
test_ok = 0
}
$5 == 200.0 {test_200++}
$5 == 300.0 {test_300++}
$5 == 500.0 {test_500++}
$5 == 1100.0 {test_1000++}
$6 !~ /SUCCESS/ {test_nok++}
$6 ~ /SUCCESS/ {test_ok++}
{count[$6]++}
NF>5&&!a[$6,$2]++{e[$6]++}
END {
for(j in count) printf "%-30s %-6d\n", j, count[j]
print ""
printf "%-30s %-6d\n", "test 200 ", test_200
printf "%-30s %-6d\n", "test 300 ", test_300
printf "%-30s %-6d\n", "test 500 ", test_500
printf "%-30s %-6d\n", "test 1000 ", test_1000
printf "\n"
printf "---------------------------------------------------\n"
printf "%-30s %-6d\n", "test unsuccessful ", test_nok
printf "%-30s %-6d\n", "test successful ", test_ok
printf "---------------------------------------------------\n"
for (i in e) printf "%-30s %-6d\n", i, e
}
' cdr
in order to process these logs:


Code:
25-04-2012;192.168.70.44;1254545454545110;300.0;SUCCESS
25-04-2012;192.168.70.31;1254545454545417;500.0;SUCCESS
30-04-2012;192.168.70.33;e;null;null;General_ERROR_23
30-04-2012;192.168.70.33;e;null;null;Failure
30-04-2012;192.168.70.33;e;null;null;Failure
30-04-2012;192.168.70.33;e;null;null;Failure
30-04-2012;192.168.70.33;e;null;null;Failure
30-04-201;192.168.70.34;e;null;null;ERROR_22
30-04-2012;192.168.70.37;e;null;null;INVALID_NUMBER
30-04-2012;192.168.70.10;e;null;null;Failure
30-04-2012;192.168.70.20;1254545454125417;1100.0;SUCCESS
The script works ok, but i want to format the output.

I want the output to be like this(2 tables, with headers and columns):



Code:
*******************************************
Message name | Number of errors | Numbers of unique IPs
*******************************************
Failure ----------------5 ------------------2----------
ERROR_22
INVALID_NUMBER
SUCCESS

*******************************************
test_200 | test_300 | test_500 | test_1000
*******************************************
0 --------------1---------1------------ 0

etc etc....
I want everything to be formated in some way.
Maybe my script need also some improvement except formatting...i dont know.

Please, take a look at my script, i really need this for my work.

Thank you in advance,
 
First, your log is dirty. The result can be in the fifth column (SUCCESS) or the sixth column (FAILURE).
Change it and you'll be able to use:

Code:
awk -F";" '{num=0;cmd="grep -c "$6" file.log";cmd|getline num;close(cmd);printf "%-20s %s\n",$6,num}' |sort -u
"-F" to change the separator
"6" is the number of the column
"sort -u" will keep only the different lines
"grep -c pattern file" can count the lines containing the pattern.
"printf" and fancier printing are explained here:
Filtering the lines with 6 columns, before the awk command above, gives:

Code:
ERROR_22             1
Failure              5
General_ERROR_23     1
INVALID_NUMBER       1

Good luck :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top