awk printf format in columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk printf format in columns
# 1  
Old 05-04-2012
awk script output

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[i]
}
' 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,


Moderator's Comments:
Mod Comment How to use code tags

Ervin

Last edited by arrals_vl; 05-04-2012 at 06:20 AM.. Reason: Please use code tags
# 2  
Old 05-04-2012
This 200/300/500/1000 is what? Would you rather count what's actually there, or just test for them to be any of those specific numbers? If you want to count each, do you want them all converted to integer first? And do you want it sorted?
# 3  
Old 05-04-2012
awk formatting

Thank you for your reply.

200,300 , 500 etc are some recharge values for mobiles.
i want to count how many 300 or 500 ...etc values are in total.
25-04-2012;192.168.70.44;1254545454545110;300.0;SUCCESS
25-04-2012;192.168.70.31;1254545454545417;500.0;SUCCESS

in my example there are in total only one for 300, and only one for 500.
For 200 and 1100 there arent any recharge values.

BUT, in fact i want to order all the results in my script in columns like i have described above. Maybe the two "for " funcions have to be in only one funcion, so i can print thei results in columns with headers.

thank you very very much for helping!!!
# 4  
Old 05-04-2012
About the "for" question --
When writing more than a one-liner, give variables clear names. You can see, you've count[] and e[] both using $6. So yes you can combine them.

Code:
*******************************************************************
        Message name |     Number of errors | Number of unique IPs
*******************************************************************
GENERAL_ERROR_23     |                    1 |                    1
ERROR_22             |                    1 |                    1
INVALID_NUMBER       |                    1 |                    1
FAILURE              |                    5 |                    2
*******************************************************************
           Recharged |      200 |      300 |      500 |     1100
                     |        0 |        1 |        1 |        1
*******************************************************************
test unsuccessful              8
test successful                3
*******************************************************************

Code:
#!/usr/bin/awk -f
BEGIN {
        FS=";"
        recharge[200]=recharge[300]=recharge[500]=recharge[1100]=0
}
NF==5 { success++ }
NF>5 { error++; $6=toupper($6); error_by_type[$6]++ }
# count unique ip:error
NF>5 && !ip_err[$6,$2]++ { error_by_ip[$6]++ }
# count recharge
(i=int($4+0.5)) && (i in recharge) { recharge[i]++ }
END {
        # make line separator
        while (length(hr) < 67) hr=hr"*"
        print hr
        printf("%20s | %20s | %20s\n", "Message name", "Number of errors",
                "Number of unique IPs");
        print hr
        for (e in error_by_type)
                printf("%-20s | %20d | %20d\n", e, error_by_type[e],
                        error_by_ip[e])
        print hr
        printf("%20s | %8d | %8d | %8d | %8d\n", "Recharged", 200,300,500,1100)
        printf("%20s | %8d | %8d | %8d | %8d\n", "",
                recharge[200],recharge[300],recharge[500],recharge[1100])
        print hr
        printf("%-30s %-6d\n", "test unsuccessful ", error)
        printf("%-30s %-6d\n", "test successful ", success)
        print hr
}

# 5  
Old 05-04-2012
perfect!

I dont know what to say...
you are a life saver!!

Thank you very veryy much!!

GOD bless you!!

THANK YOU!
# 6  
Old 05-08-2012
awk output log_$date

Hi all,

i want that the output of my awk script to be a file named log_$date.
For example the output of my script for today will be log_08052012.
How can i use the variable $date inside awk ?

Thank you for you time!
# 7  
Old 05-08-2012
Code:
 
your awk command > log_`date +%d%m%Y`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

printf format with awk

Hello Here is an easy one Data file 12345 (tab) Some text (tab) 53.432 23456 (tab) Some longer text (tab) 933.422 34567 (tab) Some different text (tab) 29.309 I need to awk these three tab-delimited columns so that the first two are unchanged (unformatted) and the third shows two decimal... (1 Reply)
Discussion started by: palex
1 Replies

2. UNIX for Dummies Questions & Answers

awk printf in required format

Hi my awk variable $0 contains the below data Input file 000001 The Data 000002* The line 2 000003* The line3 output file Req 000001* The Data 000002** The line 2 000003** The line3 one * at column seven needs to be appended to the input lines, (5 Replies)
Discussion started by: rakeshkumar
5 Replies

3. Shell Programming and Scripting

awk with printf

Hi, I am using the following code to assign a count value to a variable. But I get nothing. Do you see anything wrong here. I am new to all this. $CTR=`remsh $m -l $MACHINES{$m} -n cat $output | grep -v sent | grep \"$input\" | sort -u | awk '{print $5}'`; Upto sort - u it's... (2 Replies)
Discussion started by: nurani
2 Replies

4. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

5. Shell Programming and Scripting

printf to format lines.

Hey, I'm trying to read printf(3) and seems I need to use the * WIDTH but I'm not quite sure how to go about it. there needs to be 3 fields per line, the lines read in from file looks like this: JUICE - APPLE:JUST JUICE:7 MILK - CHOCOLATE:BREAKA:7 this needs to be output backwards with... (6 Replies)
Discussion started by: gcampton
6 Replies

6. Shell Programming and Scripting

How to format columns using AWK

Hi, How to format something like this: John Roberts 324-141-984 Acct Jack Williams 159-555-555 Acct William Jackson 949-911-888 Acct Mark J Walton 145-852-252 Acct Fred P Milton 483-244-390 Acct Bill P J Miller 404-050-223 Acct into... (12 Replies)
Discussion started by: ora_umair
12 Replies

7. Shell Programming and Scripting

How to format the output using float in awk{printf}

Hi I'm using awk to manipulate the data in the 6th field of the file xxx_yyy.hrv. The sample data that is available in this field is given below 220731.7100000000000000 When i tried using this command cat xxx_yyy.hrv | awk '{printf("%23.16f\n",$6*-1)}' I get the output as... (4 Replies)
Discussion started by: angelarosh
4 Replies

8. Shell Programming and Scripting

awk printf formatting using string format specifier.

Hi all, My simple AWK code does C = A - B If C can be a negative number, how awk printf formating handles it using string format specifier. Thanks in advance Kanu :confused: (9 Replies)
Discussion started by: kanu_pathak
9 Replies

9. Shell Programming and Scripting

KSH printf: columns and colors

Ih all, I need to make a ksh script with colors, it is possible with printf to combine column and colors ? i seem not working, I think i dont doing the good thing: printf -n "%-15s %-20s %-20s\n" "\033 the position is ok Name______Age________Site ----________---_________---- Bob... (2 Replies)
Discussion started by: wolfhurt
2 Replies

10. Shell Programming and Scripting

printf format

hi, i would like to extract the header and put it in a variable, then use printf to output the variable, but i keep on getting errors...please tell me if my format is incorrect. HDR = "`ps -e -o user,pid,ppid,pcpu,stime,etime,time,comm | head -n 1`" printf (%s, $HDR); thanks! (3 Replies)
Discussion started by: laila63
3 Replies
Login or Register to Ask a Question