Improve script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Improve script
# 15  
Old 11-23-2016
RudiC
Finally I did something to get the desired output it is long but works ...
here the script.
Code:
#!/bin/bash
dd=56
awk -F, -v CATS="-10,-5,0,15,25,35,50" '
NR==1   {CNTLM = split (CATS, LMT)                                      
        }
        {LNC[$6]++                                                      
         for (IX=1; IX<=CNTLM; IX++) if ($10 <= LMT[IX]) break          
         CNT[$6,IX]++                                                   
        }  
END     {printf "FLD6"                                                  
         for (IX=1; IX<=CNTLM; IX++) printf "%6d", LMT[IX]
         printf "" RS                          
           for (l in LNC)                                                 
                {printf "%4d", l                                         
                 for (IX=1; IX<=CNTLM; IX++) printf "%6d", CNT[l,IX] +0
                 printf RS                                         
                }
        }
' test1 > tmp2

awk 'NR>1{print}' tmp2 | sort -k1n > tmp3

#header
awk '
 NR==1{print}' tmp2 > header 
awk     'BEGIN  {DASH=sprintf("%'$dd's",_); gsub(/ /,"-",DASH); print DASH}' >> header 

#sum columns
awk '
 {for (i=2;i<=NF;i++) a[i]+=$i} 
  END{for (i=2;i<=NF;i++) printf"%6s", a[i]}' tmp3 |
  awk '{print "TbyH"$0}' > tmp4 

cat tmp3 tmp4 > tmp5 
awk 'NR>1{for(i=1;i<=NF;i++) t+=$i; print $0 "  =  " t; t=0 }' tmp5 > tmp6

awk     'BEGIN  {DASH=sprintf("%'$dd's",_); gsub(/ /,"-",DASH); print DASH}' >> tmp6

#count columns 

awk '
      {for(i=2;i<=NF;i++) a[i]+=$i>0}
      END{for(i=2;i<=NF;i++)printf"%6s", a[i]}' tmp3 |
  awk '{print "TViU"$0}' | awk 'END{print}' > tmp7 
awk     'BEGIN  {DASH=sprintf("%'$dd's",_); gsub(/ /,"-",DASH); print DASH}' >> tmp7
   
cat  header tmp6 tmp7 > out.txt
sed -i '/TbyH/ i --------------------------------------------------------' out.txt
rm -f *tmp*


the output here:

Code:
FLD6   -10    -5     0    15    25    35    50     TbyV
--------------------------------------------------------
   2     0     0     2  1091   118     9     1  =  1223
   8     0     0     4  1193     3     1     0  =  1209
   9     0     0     2  1180     3     0     0  =  1194
  10     0     0     2   750    24     3     0  =  789
  12     0     0     2   967    31     0     0  =  1012
  13     0     0     3   967     4     0     0  =  987
  14     0     0     0   369     7     1     0  =  391
  15     0     0     4   916    19     0     0  =  954
  18     0     0     2   769     6     1     0  =  796
  22     0     0     2  1034   169    12     0  =  1239
  24     0     0     3  1154    24     0     0  =  1205
  28     0     0     0  1100    48     7     1  =  1184
--------------------------------------------------------
TbyH     0     0    26 11490   456    34     2  =  12008
--------------------------------------------------------
TViU     0     0    10    12    12     7     2
--------------------------------------------------------

please try to short the script to get the desired output.

Thanks a lot.

Last edited by jiam912; 11-23-2016 at 02:13 PM..
# 16  
Old 11-23-2016
How about
Code:
awk -F, '
BEGIN   {CNTLM = split ("-10,-5,0,15,25,35,50", LMT)
#        LMT[CNTLM+1] = "50+"
         cmdsort = "sort -n"
         DASHES = sprintf ("%0*d", 8*(CNTLM+3), _)
         gsub (/0/, "-", DASHES)
        }

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

END     {printf "FLD6"
         for (IX=1; IX<=CNTLM; IX++) printf "\t<=%s", LMT[IX]
         printf "\t>%d\t  TbyV" RS, LMT[--IX]
         print DASHES

         for (l in LNC)
                {printf "%d", l | cmdsort
                 for (IX=1; IX<=CNTLM+1; IX++)  {printf "\t%d", CNT[l,IX]+0 | cmdsort
                                                 NNUL[IX] += CNT[l,IX]>0
                                                }
                 printf "\t= %d" RS, LNC[l] | cmdsort
                }
         close (cmdsort)

         print DASHES
         printf "TbyH"
         for (IX=1; IX<=CNTLM+1; IX++) printf "\t%d", COLTOT[IX]+0
         printf "\t= %d" RS, TOT

         print DASHES
         printf "TViU"
         for (IX=1; IX<=CNTLM+1; IX++) printf "\t%d", NNUL[IX]
         printf RS
        }
'  tmp1
FLD6	<=-10	<=-5	<=0	<=15	<=25	<=35	<=50	>50	  TbyV
--------------------------------------------------------------------------------
2	0	0	2	1091	118	9	1	0	= 1221
8	0	0	4	1193	3	1	0	0	= 1201
9	0	0	2	1180	3	0	0	0	= 1185
10	0	0	2	750	24	3	0	0	= 779
12	0	0	2	967	31	0	0	0	= 1000
13	0	0	3	967	4	0	0	0	= 974
14	0	0	0	369	7	1	0	0	= 377
15	0	0	4	916	19	0	0	0	= 939
18	0	0	2	769	6	1	0	0	= 778
22	0	0	2	1034	169	12	0	0	= 1217
24	0	0	3	1154	24	0	0	0	= 1181
28	0	0	0	1100	48	7	1	0	= 1156
--------------------------------------------------------------------------------
TbyH	0	0	26	11490	456	34	2	0	= 12008
--------------------------------------------------------------------------------
TViU	0	0	10	12	12	7	2	0

This User Gave Thanks to RudiC For This Post:
# 17  
Old 11-24-2016
Thanks many thanks....

It is awesome... Appreciate your help..

---------- Post updated 11-24-16 at 05:59 AM ---------- Previous update was 11-23-16 at 03:15 PM ----------

Hi RudiC,

it is possible to replace only in the selection (values in blue). Values which are = 0 using this code.
Code:
awk '{gsub(" 0"," -")}'

.
I was trying to adapt it to the script but i cant. Smilie
Code:
FLD6	<=-10	<=-5	<=0	<=15	<=25	<=35	<=50	>50	  TbyV
--------------------------------------------------------------------------------
2	0	0	2	1091	118	9	1	0	= 1221
8	0	0	4	1193	3	1	0	0	= 1201
9	0	0	2	1180	3	0	0	0	= 1185
10	0	0	2	750	24	3	0	0	= 779
12	0	0	2	967	31	0	0	0	= 1000
13	0	0	3	967	4	0	0	0	= 974
14	0	0	0	369	7	1	0	0	= 377
15	0	0	4	916	19	0	0	0	= 939
18	0	0	2	769	6	1	0	0	= 778
22	0	0	2	1034	169	12	0	0	= 1217
24	0	0	3	1154	24	0	0	0	= 1181
28	0	0	0	1100	48	7	1	0	= 1156
--------------------------------------------------------------------------------
TbyH	0	0	26	11490	456	34	2	0	= 12008
--------------------------------------------------------------------------------
TViU	0	0	10	12	12	7	2	0

# 18  
Old 11-24-2016
Show your adaption so we can discuss. Please note there's no such string " 0" in the output so your gsub must fail.

And - DON'T pipe through a new awk script - it can and should be done in the original script.
# 19  
Old 11-27-2016
RudiC,
I add the average at the script for all columns.
Code:
          print DASHES
         printf "TbyH"
         for (IX=1; IX<=CNTLM+1; IX++) printf "\t%2.1f", COLTOT[IX]/TOT*100

Please can you add min and max for all columns... Please

---------- Post updated at 11:41 AM ---------- Previous update was at 11:37 AM ----------

I found this code... But will be nice if you add this output in your code..
Code:
 awk 'NR==1{for (i=2; i<=NF; i++) {min[i]=$i}; next}
     {for (i=2; i<=NF; i++) { min[i]=(min[i]>$i?$i:min[i]); max[i]=(max[i]<$i?$i:max[i]) }}
     END {printf "min: "; for (i=1;i<=NF;i++) printf "%g %s", min[i], (i==NF?"\n":" "); 
          printf "max: "; for (i=1;i<=NF;i++) printf "%g %s", max[i], (i==NF?"\n":" ")}'

# 20  
Old 11-27-2016
I had the impression that you might have understood the little program, seconded by the proposal you made for the average (except for the too small float format). What keeps you from applying the same structure to the min/max problem? And, what about the dash replacement for the zeroes?
# 21  
Old 11-27-2016
Hi RudiC

I try to do it
Code:
         for (l in LNC)
                {printf "%d", l | cmdsort
                 for (IX=1; IX<=CNTLM+1; IX++)  {printf "\t%d", CNT[l,IX]+0 | cmdsort
                                                 NNUL[IX] += CNT[l,IX]>0
                                                 MIN[IX]=$IX
                                                 MAX[IX]=$IX

Code:
         print DASHES
         printf "TbyM"
         for (IX=1; IX<=CNTLM+1; IX++) printf "\t%d", MIN[IX]

          print DASHES
         printf "TbyM"
         for (IX=1; IX<=CNTLM+1; IX++) printf "\t%d", MAX[IX]

But it does not work Smilie

about the dash replacement for the zeroes.. i try to use the
Code:
gsub

but it fails ...
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