Need help in awk: running a loop with one column and segregate data 4 each uniq value in that field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in awk: running a loop with one column and segregate data 4 each uniq value in that field
# 1  
Old 01-19-2018
Need help in awk: running a loop with one column and segregate data 4 each uniq value in that field

Hi All,

I have a file like this(having 2 column).

Column 1: like a,b,c....
Column 2: having numbers.

I want to segregate those numbers based on column 1.

Example:

file.
Code:
a 5
b 9
b 620
a 710
b 230
a 330
b 1910
a 2020

for each value of column 1, these data required.
Code:
greater than 0 and less than 600
greater than 600 and less than 1800
greater than 1800

Output should be like this.
Code:
a[0-600] = 2
a[600-1800] = 1
a[above 1800] = 1
b[0-600] = 2
b[600-1800] = 1
b[above 1800] = 1

I can do that with help of for loop, but I want to know, how to do this with just AWK.

Code:
for i in `awk '{print $1}' /home/ABC/gtacMonitoring/monLogs/ngnDurationLog|sort|uniq`
do
echo "$i"
awk -v var="$i" '{if($1==var && ($2 > 0 && $2 < 600)) {print "[Between 00-10 min]:"} else if($1==var && ($2 >= 600 && $2 < 1800)) {print "[Between 10-30 min]:"} else if($1==var && $2 >= 1800) {print "[More than  30 min]:"}}' /home/ABC/gtacMonitoring/monLogs/ngnDurationLog|sort|uniq -c|awk -F"[" '{print "["$2,$1}'
echo
done


Last edited by Scott; 01-19-2018 at 08:55 AM.. Reason: Use code tags, please...
# 2  
Old 01-19-2018
You're close. Did you consider deploying arrays?
Will negative values be present in field (COL) 2?
# 3  
Old 01-19-2018
Hi Rudi,

Thank you for response.

I understand that it can be done with array, but not able to use logic with same.
Column 2 will not contain any negative value, in fact it will be always greater than 0.

Regards,
Raza Ali
# 4  
Old 01-19-2018
Try

Code:
awk -v THRSTR="600 1800" '
BEGIN   {n = split (THRSTR, THR)
        }
        {for (i=1; i<=n; i++)   {if ($2 <= THR[i]) break
                                }
         RES[$1,i]++
        }

END     {for (r in RES) {split (r, T, SUBSEP)
                         print T[1], "[" THR[T[2]-1] "-" THR[T[2]] "] = " RES[r]
                        }
        }
'   file

a [-600] = 2
a [600-1800] = 1
a [1800-] = 1
b [-600] = 2
b [600-1800] = 1
b [1800-] = 1

Your request leaves open where to count the exact boundary values - here I count them to the lower interval, i.e. 600 goes to 0 - 600, 601 to 600 - 1800.

EDIT:
Oh, I see that your code snippet handles it vice versa. Exercise for you to adapt the proposal . . .

Last edited by RudiC; 01-19-2018 at 08:47 PM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 01-20-2018
Hi Rudi,

Thank you for providing the solution.

Regards,
Raza Ali
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bring values in the second column into single line (comma sep) for uniq value in the first column

I want to bring values in the second column into single line for uniq value in the first column. My input jvm01, Web 2.0 Feature Pack Library jvm01, IBM WebSphere JAX-RS jvm01, Custom01 Shared Library jvm02, Web 2.0 Feature Pack Library jvm02, IBM WebSphere JAX-RS jvm03, Web 2.0 Feature... (10 Replies)
Discussion started by: kchinnam
10 Replies

2. Shell Programming and Scripting

Running awk and while loop on data

so this is what i want to do. i have data that looks like this: DATA: Array: A Interface Type: SAS Unused Space: 0 MB Status: Failed Logical Drive: 2 Size: 279.4 GB Fault Tolerance: RAID 1 Heads: 255 Sectors Per Track: 32... (2 Replies)
Discussion started by: SkySmart
2 Replies

3. Shell Programming and Scripting

Printing uniq first field with the the highest second field

Hi All, I am searching for a script which will produce an output file with the uniq first field with the second field having highest value among all the duplicates.. The output file will produce only the uniqs which are duplicate 3 times.. Input file X 9 B 5 A 1 Z 9 T 4 C 9 A 4... (13 Replies)
Discussion started by: ailnilanjan
13 Replies

4. Shell Programming and Scripting

Sort field and uniq

I have a flatfile A.txt 2012/12/04 14:06:07 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 17:07:22 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 17:13:27 |trees|Boards 2, 3|denver|mekong|mekong12 2012/12/04 14:07:39 |rain|Boards 1|tampa|merced|merced11 How do i sort and get... (3 Replies)
Discussion started by: sabercats
3 Replies

5. Shell Programming and Scripting

awk uniq and longest string of a column as index

I met a challenge to filter ~70 millions of sequence rows and I want using awk with conditions: 1) longest string of each pattern in column 2, ignore any sub-string, as the index; 2) all the unique patterns after 1); 3) print the whole row; input: 1 ABCDEFGHI longest_sequence1 2 ABCDEFGH... (12 Replies)
Discussion started by: yifangt
12 Replies

6. Shell Programming and Scripting

loop in awk - column max for each column

Hello all, this should really be easy for you... I need AWK to print column maxima for each column of such input: Input: 1 2 3 1 2 1 1 3 2 1 1 2 Output should be: 2 2 3 3 This does the sum, but i need max instead: { for(i=1; i<=NF; i++) sum +=$i } END {for(i=1; i in sum;... (3 Replies)
Discussion started by: irrevocabile
3 Replies

7. Shell Programming and Scripting

How to use uniq on a certain field?

How can I use uniq on a certain field or what else could I use? If I want to use uniq on the second field and the output would remove one of the lines with a 5. bob 5 hand jane 3 leg jon 4 head chris 5 lungs (1 Reply)
Discussion started by: Bandit390
1 Replies

8. Shell Programming and Scripting

How to replicate data using Uniq or awk

Hi, I have this scenario; where there are two classes:- apple and orange. 1,2,3,4,5,6,apple 1,1,0,4,2,3,apple 1,3,3,3,3,4,apple 1,1,1,1,1,1,orange 1,2,3,1,1,1,orange Basically for apple, i have 3 entries in the file, and for orange, I have 2 entries. Im trying to edit the file and find... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

9. UNIX for Dummies Questions & Answers

How to uniq third field in a file

Hi ; I have a question regarding the uniq command in unix How do I uniq 3rd field in a file ? original file : zoom coord 39 18652 39 18652 zoom coord 39 18653 39 18653 zoom coord 39 18818 39 18818 zoom coord 39 18840 39 18840 zoom coord 41 15096 41 15096 zoom... (1 Reply)
Discussion started by: babycakes
1 Replies

10. UNIX for Dummies Questions & Answers

Uniq using only the first field

Hi all, I have a file that contains a list of codes (shown below). I want to 'uniq' the file using only the first field. Anyone know an easy way of doing it? Cheers, Dave ##### Input File ##### 1xr1 1xws 1yxt 1yxu 1yxv 1yxx 2o3p 2o63 2o64 2o65 1xr1 1xws 1yxt 1yxv 1yxx 2o3p 2o63 2o64... (8 Replies)
Discussion started by: Digby
8 Replies
Login or Register to Ask a Question