Help for a awk script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help for a awk script
# 1  
Old 04-16-2012
Power Help for a awk script

Hi comunity, its my first thread. I'm working in a script for sort a lot data of next way:

Code:
ID MINUTE TIME 
SA 17:21:34 507
SB 17:22:04 484
ST 17:24:49 431
SB 17:28:16 546
SA 17:28:35 304
SA 17:29:24 437
SY 17:36:45 521
SX 17:38:47 277
SX 17:38:59 517
SB 17:42:57 249
ST 17:43:25 587
SY 17:44:51 575
ST 17:49:31 624

and i'm specting the result be sort the total ids "S'X'" printing the totals in, them take the average of time for every ID, the max time , the less time. i.e:
Code:
ID TOTAL AVERAGE MAX-TIME MIN-TIME
SA 3 430 507 304
SB 3 470 546 249
ST 3 521 624 431
SX 2 393 517 277
SY 2 543 575 521

I was looking for the before opened threads but at moment i didn't this work. I will greatest for any help!Smilie


Regards!
Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-16-2012 at 01:06 PM.. Reason: code tags
# 2  
Old 04-16-2012
Taking the "average of time for every ID" isnt making any sense...so can you explain with an example.
# 3  
Old 04-16-2012
Hi how far did you get with your script and what didn't work?
# 4  
Old 04-16-2012
Hi, first thanks for answer!

well, i was looking for this script in thread awk array sort
Code:
#!/bin/bash
echo -e "\t\tDATE\tDIR1\tDIR2\tDIR3\tDIR4\n"
for date in `awk -F, '{ print $1 }' file | sort | uniq`
 do
 printf "%20s" $date
 for dir in `awk -F, '{ print $2 }' file | sort | uniq `;
  do
    val=`grep -E "$date.*$dir" file | awk -F, '{ print $3 }'`
    printf "%8d" $val
  done
  echo -e "\n"
done

I' m so beginer in awk, but we can say "i indurstand logic programation" but im not a programer... Smilie

Later i was probing the next:

Code:
TIME_AVERAGE=`awk '{sum+=$3} END {print sum/NR}' $PURE_LOG`
TIME_MAX=`awk '$3 > max { max=$3; linea=$0 }; END { print max}' $PURE_LOG`
TIME_MIN=`awk 'BEGIN{min=512} {  if(min > $3){ min = $3 } } END{ print  min}' $PURE_LOG`
MINUTO=`cat $PURE_LOG | awk '{print $2}' | cut -c 4-5 | sort -n`
ID=`(here i stay freezed)` i will take the "uniq" id 
SUM_ID=`(here too im freeze) here i will take the sumatorie id per id
echo "
echo -e "ID\tTOTAL OPERATIONS\tTIME AVERAGE\tMAX TIME\tMIN TIME\n"

awk '{printf $ID SUM_ID TIME_AVERAGE TIME_MAX TIME_MIN}'


Thank's so much, and sorry for my englishSmilie

Moderator's Comments:
Mod Comment Code tags for code, please. Color will in fact work inside code...

Last edited by Corona688; 04-16-2012 at 02:52 PM..
# 5  
Old 04-16-2012
Since you have lots of values, we should avoid calling external programs, especially in the loop. This has no external programs in the loop and should run quite a bit faster, try and see if this works..

Code:
sort infile | 
{
  while read newid x val
  do
    case $newid in                                
      ID)    continue ;;                           
  
      "$id") count=$((count + 1))
             tot=$((tot + val))
             if [ $val -gt $max ]; then            
               max=$val
             elif [ $val -lt $min ]; then
               min=$val
             fi ;;
  
      ?*)    if [ -n "$id" ]; then                 
               printf "%s %d %d %d %d\n" "$id" $count $(( tot/count )) $max $min
             fi
             min=$val max=$val tot=$val            
             count=1
             id=$newid ;;
    esac
  done
  printf "%s %d %d %d %d\n" "$id" $count $(( tot/count )) $max $min
}

If this is not fast enough, then it can be changed to awk, but you seem more comfortable with shell..

Last edited by Scrutinizer; 04-16-2012 at 05:01 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 04-16-2012
Thaks so very much scrutinizer, i will test and them post the results!

Regards!!!
# 7  
Old 04-18-2012
Thanks Scrutnizer, but o forget explain wath the "ID's" are not begined by "x" it is variable and mean start by y, a, s, etc. I was traing make a uniq with awk, but again i dont have good fruits... :S The problem is im new in shell scripting, im studing all nights later work, but im still in a low level...

Im sorry, i promess give help when i have sucessfull aknowledgments!

---------- Post updated 04-18-12 at 02:00 AM ---------- Previous update was 04-17-12 at 05:27 PM ----------

Im sorry, i didn't see but the script run equal taking any ID (i was tinking it just mean run if the it begin with "x" but the "x" run for any ID) Im really sorry... Smilie Thanks for your help!

if it is not trouble, mean you helpme how to sort only for day? I mean, this is a log generated wath not is rotated, so, i will run this in a cron daily (asumme every 24 hours) and i will take only the date of the day. I was trying to develope some but dont works...As i tell you im very beginer (very very so so bad) programing...


Thanks for your help!

Regards!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to call and sort awk script and output

I'm trying to create a shell script that takes a awk script that I wrote and a filename as an argument. I was able to get that done but I'm having trouble figuring out how to keep the header of the output at the top but sort the rest of the rows alphabetically. This is what I have now but it is... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. Shell Programming and Scripting

awk script to call another script based on second column entry

Hi I have a text file (Input.txt) with two column entries separated by tab as given below: aaa str1 bbb str2 cccccc str3 dddd str4 eee str3 ssss str2 sdf str3 hhh str1 fff str2 ccc str3 ..... ..... ..... (1 Reply)
Discussion started by: my_Perl
1 Replies

3. UNIX for Dummies Questions & Answers

Passing shell script parameter value to awk command in side the script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies

4. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

5. Shell Programming and Scripting

Calling shell script within awk script throws error

I am getting the following error while passing parameter to a shell script called within awk script. Any idea what's causing this issue and how to ix it ? Thanks sh: -c: line 0: syntax error near unexpected token `newline' sh: -c: line 0: `./billdatecalc.sh ... (10 Replies)
Discussion started by: Sudhakar333
10 Replies

6. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

7. Shell Programming and Scripting

Help: How to convert this bash+awk script in awk script only?

This is the final first release of the dynamic menu generator for pekwm (WM). #!/bin/bash function param_val { awk "/^${1}=/{gsub(/^${1}="'/,""); print; exit}' $2 } echo "Dynamic {" for CF in `ls -c1 /usr/share/applications/*.desktop` do name=$(param_val Name $CF) ... (3 Replies)
Discussion started by: alexscript
3 Replies

8. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

9. Shell Programming and Scripting

want to pass parameters to awk script from shell script

Hello, I have this awk script that I want to execute by passing parameters through a shell script. I'm a little confused. This awk script removes duplicates from an input file. Ok, so I have a .sh file called rem_dups.sh #!/usr/bin/sh... (4 Replies)
Discussion started by: script_op2a
4 Replies

10. Shell Programming and Scripting

create a shell script that calls another script and and an awk script

Hi guys I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file. #!/bin/sh echo " $2 $3 $4 $5 $6 $7 isql -w400 -U$2 -S$5 -P$3 << xxx use $4 go print"**Changes to the table... (0 Replies)
Discussion started by: magikminox
0 Replies
Login or Register to Ask a Question