Improve script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Improve script
# 1  
Old 11-18-2016
Improve script

Gents,

Is there the possibility to improve this script to be able to have same output information.

I did this script, but I believe there is a very short code to get same output

here my script

Code:
awk -F, '{if($10>0 && $10<=15) print $6}' tmp1 | sort -k1n | awk '{a[$1]++} END { for (n in a ) print n, a[n]}' tmp1 > tmp2
awk -F, '{if($10>15.01 && $10<=25) print $6}' tmp1 | sort -k1n | awk '{a[$1]++} END { for (n in a ) print n, a[n]}' tmp1 > tmp3
awk -F, '{if($10>25.01 && $10<=35) print $6}' tmp1 | sort -k1n | awk '{a[$1]++} END { for (n in a ) print n, a[n]}'tmp1 > tmp4
awk -F, '{if($10>35.01 && $10<=50) print $6}' tmp1 | sort -k1n | awk '{a[$1]++} END { for (n in a ) print n, a[n]}' tmp1 > tmp5
awk -F, '{if($10>50.01 print $6}' tmp1 | sort -k1n | awk '{a[$1]++} END { for (n in a ) print n, a[n]}' tmp1 > tmp6

clear

awk 'NR==FNR{a[$1]=$2;next}
    {if($1 in a){print $0,a[$1];delete a[$1]}
        else print $0,"0"}
    END{for(x in a)print x,"0",a[x]}' tmp2 tmp3 | sort -k1n > tmp1a

awk 'NR==FNR{a[$1]=$2;next}
    {if($1 in a){print $0,a[$1];delete a[$1]}
        else print $0,"0"}
    END{for(x in a)print x,"0",a[x]}' tmp2 tmp4 | sort -k1n > tmp2a


awk 'NR==FNR{a[$1]=$2;next}
    {if($1 in a){print $0,a[$1];delete a[$1]}
        else print $0,"0"}
    END{for(x in a)print x,"0",a[x]}' tmp2 tmp5 | sort -k1n > tmp3a

awk 'NR==FNR{a[$1]=$2;next}
    {if($1 in a){print $0,a[$1];delete a[$1]}
        else print $0,"0"}
    END{for(x in a)print x,"0",a[x]}' tmp2 tmp6 | sort -k1n > tmp4a


paste tmp1a tmp2a tmp3a tmp4a > tmp1b

awk '{
	printf("%5s%10s%10s%10s%10s%10s\n",
		$1,$3,$2,$5,$8,$11)}' tmp1b > tmp10

the output i got:
Code:
    2      1277       123        10         1         0
    3       678        16         0         0         0
    6      1344        14         2         0         0
    8      1320         5         1         0         0
    9      1349         3         0         0         0
   10       880        34         3         0         0
   11      1380        28         0         0         0
   12       967        31         0         0         0
   13      1094         6         1         0         0
   14       832        27         1         0         0
   15      1103        33         1         0         0
   16      1485        14         1         0         0
   17       716       651        10         0         0
   18       959        16         2         0         0
   19      1493         7         1         0         0
   20      1234        28         0         0         0
   21       350         9         0         0         0
   22      1213       177        13         0         0
   24      1324        25         0         0         0
   25       276      1081         5         0         0
   26      1389        39         4         0         0
   27       282      1030         3         0         0
   28      1251        73         7         1         0

input file attached. ( I have removed some lines from the file to short the size )

Thanks for your help.Smilie
# 2  
Old 11-18-2016
Would you mind explaining what you're actually doing? I can see that the first 5 awk/sort/awk/kitchen/sinks are separating and counting ranges but aren't sure about the rest.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-18-2016
Hi.
Based in the column 6 i will like to count how many times are repeated values in the column 10 for different ranges.. There 5 ranges :
Code:
$10>0 && $10<=15
$10>15.01 && $10<=25
$10>25.01 && $10<=35
$10>35.01 && $10<=50
$10>50.01

# 4  
Old 11-18-2016
But your script doesn't check for repeats in column 10? I don't understand.

I also don't understand what the second half of your script is for.
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 11-18-2016
Dear Corona688.
There is to conditions...
Count the values in column10 for each value in column 6 and split according to the 5 conditions.

The second part of the script is to merge the conditions in single output.
# 6  
Old 11-18-2016
Although what I see is way beyond my grasp, I'd almost bet that the entire script can be done in a single awk command. Please explain step by step, seconded by (interim) data, what the processing should look like.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-18-2016
Hi RudiC,

Yes, I believe the output generated with all my script can be done with only single awk command,. but i cant Smilie.

Exactly what i wan is using the file attached.

In column 10 have some values which I want to separate as columns using the 5 conditions.

Code:
$10>0 && $10<=15
$10>15.01 && $10<=25
$10>25.01 && $10<=35
$10>35.01 && $10<=50
$10>50.01

That means i need to have 5 columns.

The other step is to count how many times for each value in column 6 has each range .. ( FYI , to be able to upload the file i reduce the lines in the input file.. then, the output results can change )

example
value 2 in column 6 has five columns which correspond to each condition
Code:
      >0-<=15   >15.01-<=25    >25.01-<=35   >35.01-<=50   >50.01
2       1277         123              10            1          0

Then, i should get something like this:
Code:
      >0-<=15   >15.01-<=25    >25.01-<=35   >35.01-<=50   >50.01
 2      1277        123             10            1         0
 3       678         16              0            0         0
 6      1344         14              2            0         0
 8      1320          5              1            0         0
 9      1349          3              0            0         0
10       880         34              3            0         0
11      1380         28              0            0         0
12       967         31              0            0         0
13      1094          6              1            0         0
14       832         27              1            0         0
15      1103         33              1            0         0
16      1485         14              1            0         0
17       716        651             10            0         0
18       959         16              2            0         0
19      1493          7              1            0         0
20      1234         28              0            0         0
21       350          9              0            0         0
22      1213        177             13            0         0
24      1324         25              0            0         0
25       276       1081              5            0         0
26      1389         39              4            0         0
27       282       1030              3            0         0
28      1251         73              7            1         0

appreciate your help.
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