Improve script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Improve script
# 8  
Old 11-18-2016
OK, ... try
Code:
awk -F, '
BEGIN   {CNTLM = split ("15,25,35,50", LMT)
#        LMT[CNTLM+1] = "50+"
        }

        {LNC[$6]++
#        TOT++
         for (IX=1; IX<=CNTLM; IX++) if ($10 <= LMT[IX]) break
         CNT[$6,IX]++ 
        }


END     {for (l in LNC)
                {printf "%d", l
                 for (IX=1; IX<=CNTLM+1; IX++) printf "\t%d", CNT[l,IX]+0
                 printf RS
#                TOT2 += LNC[l]
                }
#        print TOT, TOT2
        }
'  tmp1
10      752     24      3       0       0
12      969     31      0       0       0
13      970     4       0       0       0
14      369     7       1       0       0
22      1036    169     12      0       0
15      920     19      0       0       0
24      1157    24      0       0       0
18      771     6       1       0       0
28      1100    48      7       1       0
2       1093    118     9       1       0
8       1197    3       1       0       0
9       1182    3       0       0       0

This is based on the assumption that
- there are no negative $10 values as the lower limit 0 is not tested
- no $10 value falls into the gap between, say, 50 and 50.01, as all these would not be counted in your script

For a reasonable header, you may want to add
Code:
	 printf "FLD6"
	 for (IX=1; IX<=CNTLM; IX++) printf "\t<=%s", LMT[IX]
	 printf "\t>%d" RS, LMT[--IX]

to the front of the END section.

Last edited by RudiC; 11-18-2016 at 04:31 PM..
This User Gave Thanks to RudiC For This Post:
# 9  
Old 11-18-2016
Amazing.. really appreciate.
Kindly, It is possible to explain the code..
# 10  
Old 11-18-2016
Code:
awk -F, '
BEGIN   {CNTLM = split (CATS, LMT)                                      # prepare the limits array for the categories given as a script parameter below
        }

        {LNC[$6]++                                                      # line count of $6 occurrences, also used for later printing the $6 values 
         for (IX=1; IX<=CNTLM; IX++) if ($10 <= LMT[IX]) break          # calculate the column index based on a comparison between $10 and the limits array
         CNT[$6,IX]++                                                   # increment the counter for $6 value and category index (if $10 greater than 50, IX == 5)
        }


END     {printf "FLD6"                                                  # create header's first element
         for (IX=1; IX<=CNTLM; IX++) printf "\t<=%s", LMT[IX]           # and further elements i.e. less or equal category limits
         printf "\t>%d" RS, LMT[--IX]                                   # last: greater than largest limit

         for (l in LNC)                                                 # now: create lines for each $6 value (taken from LNC array)
                {printf "%d", l                                         # print the $6 value as the leftmost column
                 for (IX=1; IX<=CNTLM+1; IX++) printf "\t%d", CNT[l,IX] # print the respective category's count for this $6 value
                 printf RS                                              # new line
                }
        }
' CATS="15,25,35,50" tmp1                                               # supply the categories and the input file

EDIT:
Small error, sorry: Defining a variable as a parameter AFTER the script makes it unavailable in the BEGIN section. Either define it using the -v option before the script, or replace the BEGIN with the NR==1 pattern.

Last edited by RudiC; 11-19-2016 at 07:20 AM..
This User Gave Thanks to RudiC For This Post:
# 11  
Old 11-19-2016
Dear RudiC.

Thanks a lot for the explanation.. really appreciate that.

Is there the option adapt the script to count even negative values in column $10?.
# 12  
Old 11-19-2016
How about trying to introduce negative limits in the CATS variable?

Code:
awk -F, -v CATS="-10,-5,0,15,25,35,50" '
.
.
.
FLD6    <=-10   <=-5    <=0     <=15    <=25    <=35    <=50    >50
2       0       1       2       1090    118     9       1       0
8       2       0       4       1191    3       1       0       0
.
.
.
22      1       0       2       1033    169     12      0       0


Last edited by RudiC; 11-19-2016 at 07:16 AM..
This User Gave Thanks to RudiC For This Post:
# 13  
Old 11-22-2016
Thanks a lot RudiC

it is possible to add the sum for all columns ans also the sum for all rows.

---------- Post updated at 06:12 AM ---------- Previous update was at 06:10 AM ----------

Code:
awk '{ 
  TR=0
  for( I = 1; I <= NF; I++ ) {
    TR += $I
    TC[I] += $I
    printf( "%6d", $I )
  }
  print "  = " TR
  TF = NF
}

END {
  for( I = 1; I <= TF; I++ )
    printf "%6d", TC[I]
  print ""
}

' file
# 14  
Old 11-22-2016
In post#8, the total for the entire file was already in, albeit commented out. Why don't you - just for the exercise - try to implement your new request and post it here so we can discuss if need be?
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Improve script and get new output file

Gents, Using the following script, I got the changes as desired in the output file called (spread_2611.x01.new). Complete file as input (spread_2611.x01). Can you please have a look to my script and improve it please. :b: Also I would like to I get a additional selecting only the records... (21 Replies)
Discussion started by: jiam912
21 Replies

2. Shell Programming and Scripting

How to improve an script?

Gents. I have 2 different scripts for the same purpose: raw2csv_1 Script raw2csv_1 finish the process in less that 1 minute raw2csv_2 Script raw2csv_2 finish the process in more that 6 minutes. Can you please check if there is any option to improve the raw2csv_2. To finish the job... (4 Replies)
Discussion started by: jiam912
4 Replies

3. Shell Programming and Scripting

Improve sftp script

Dear all, I have written two scripts to transfer files to another server outside the company. One is a batch script , and the other script calls the batch script, send the files and archive the file sent. The problem is, that I want to get the list of files which have been uploaded the the... (10 Replies)
Discussion started by: arrals_vl
10 Replies

4. UNIX for Dummies Questions & Answers

How to improve the performance of this script?

Hi , i wrote a script to convert dates to the formate i want .it works fine but the conversion is tkaing lot of time . Can some one help me tweek this script #!/bin/bash file=$1 ofile=$2 cp $file $ofile mydates=$(grep -Po '+/+/+' $ofile) # gets 8/1/13 mydates=$(echo "$mydates" | sort |... (5 Replies)
Discussion started by: vikatakavi
5 Replies

5. Shell Programming and Scripting

Var Check Script (Help improve if possible)

I am working on a script to check the var on all of my systems. Can someone help me fix it to work better or give me suggestions. #!/bin/ksh IN=/path/to/list_of_workstations.txt while read hostnames do if ping $hostnames 1 | grep alive > /dev/null then percent=`ssh -q... (3 Replies)
Discussion started by: whotippedmycow
3 Replies

6. UNIX for Dummies Questions & Answers

[please] improve my shell/SQL*Plus script

Hi We generate with PL/SQL *.csv files, archive them and mail to the customer. Here is my script (Solaris 10, ksh): #!/bin/ksh # Unix Shell Script Structure for PL/SQL queries with SQL*Plus . ~/.profile scriptdir=/opt/ora/scripts queryname1=example... (1 Reply)
Discussion started by: slashdotweenie
1 Replies

7. Shell Programming and Scripting

Want to improve the performance of script

Hi All, I have written a script as follows which is taking lot of time in executing/searching only 3500 records taken as input from one file in log file of 12 GB Approximately. Working of script is read the csv file as an input having 2 arguments which are transaction_id,mobile_number and search... (6 Replies)
Discussion started by: poweroflinux
6 Replies

8. Shell Programming and Scripting

Improve the performance of a shell script

Hi Friends, I wrote the below shell script to generate a report on alert messages recieved on a day. But i for processing around 4500 lines (alerts) the script is taking aorund 30 minutes to process. Please help me to make it faster and improve the performace of the script. i would be very... (10 Replies)
Discussion started by: apsprabhu
10 Replies

9. Shell Programming and Scripting

Any way to improve performance of this script

I have a data file of 2 gig I need to do all these, but its taking hours, any where i can improve performance, thanks a lot #!/usr/bin/ksh echo TIMESTAMP="$(date +'_%y-%m-%d.%H-%M-%S')" function showHelp { cat << EOF >&2 syntax extreme.sh FILENAME Specify filename to parse EOF... (3 Replies)
Discussion started by: sirababu
3 Replies

10. Shell Programming and Scripting

Can I improve this script ???

Hi all, Still a newbie and learning as I go ... as you do :) Have created this script to report on disc usage and I've just included the ChkSpace function this morning. It's the first time I've read a file (line-by-bloody-line) and would like to know if I can improve this script ? FYI - I... (11 Replies)
Discussion started by: Cameron
11 Replies
Login or Register to Ask a Question