Emulate group-by in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Emulate group-by in shell script
# 1  
Old 03-22-2017
Emulate group-by in shell script

Hello All,

I saw this problem on one of the forum and solved it using group-by in oracle sql, though I am a bit curious to implement it using shell script :

There is a file having number of operations :
Code:
Opeation,Time-Taken
operation1,83621
operation2,72321
operation3,13288
operation2,12312
operation1,12321
operation2,45455
operation2,42543
operation1,87934
operation4,94865
operation5,27383
operation6,322
operation6,93483
operation7,3223

My task is to find the min and max time taken by each operation. Expected output :
Code:
operation1,12321,87934
operation2,12312,72321
operation3,13288,13288
operation4,94865,94865
operation5,27383,27383
operation6,322,93483
operation7,3223,3223

I have tried solving it using combination of sort and uniq but not successful :
Code:
sort -nk 2 | uniq

Any help is highly appreciated.
# 2  
Old 03-22-2017
Hi, try:
Code:
awk '
  NR==1 {
    next
  }

  !($1 in L) {
    L[$1]=$2
  }

  $2<L[$1] {
    L[$1]=$2
  } 

  $2>=H[$1] {
    H[$1]=$2
  }

  END {
    for(i in L) print i, L[i], H[i]
  }
' FS=, OFS=,  file


Last edited by Scrutinizer; 03-24-2017 at 02:14 AM.. Reason: Added header skip
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-22-2017
awk -F, -f muk.awk myFile OFS=, where muk.awk is:
Code:
FNR>1 {
   if (!($1 in amin))
       amin[$1]=amax[$1]=$2
   else {
      if($2<amin[$1]) amin[$1]=$2
      if($2>amax[$1]) amax[$1]=$2
   }
}
END {
  for (i in amin)
    print i,amin[i], amax[i]
}

This User Gave Thanks to vgersh99 For This Post:
# 4  
Old 03-23-2017
Code:
perl -nalF',' -e '
 print "@F[0],MIN,MAX" and next if $. == 1;
 push @{$o{$F[0]}}, $F[1];
 END{ for( sort keys %o ){ @so = sort @{ $o{$_} };
      $l = $so[-1] ? $so[-1] : $so[0];
      print "$_,$so[0],$l" }
 }' mukulverma2408.file


Output:

Code:
Opeation,MIN,MAX
operation1,12321,87934
operation2,12312,72321
operation3,13288,13288
operation4,94865,94865
operation5,27383,27383
operation6,322,93483
operation7,3223,3223

This User Gave Thanks to Aia For This Post:
# 5  
Old 03-23-2017
Hi.

As with other non-oracle solutions such as awk, perl, here is datamash:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate statistics for grouped data, datamash.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C datamash

FILE=${1-data1}
E=expected-output.txt

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat $E

pl " Results:"
datamash -t',' --sort --group=1 min 2 max 2 < data1 |
tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C || ( pe; pe " Results cannot be verified." ) >&2

pl " Details for datamash:"
dixf datamash

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
datamash (GNU datamash) 1.0.6

-----
 Input data file data1:
operation1,83621
operation2,72321
operation3,13288
operation2,12312
operation1,12321
operation2,45455
operation2,42543
operation1,87934
operation4,94865
operation5,27383
operation6,322
operation6,93483
operation7,3223

-----
 Expected output:
operation1,12321,87934
operation2,12312,72321
operation3,13288,13288
operation4,94865,94865
operation5,27383,27383
operation6,322,93483
operation7,3223,3223

-----
 Results:
operation1,12321,87934
operation2,12312,72321
operation3,13288,13288
operation4,94865,94865
operation5,27383,27383
operation6,322,93483
operation7,3223,3223

-----
 Verify results if possible:

-----
 Comparison of 7 created lines with 7 lines of desired results:
 Succeeded -- files (computed) f1 and (standard) expected-output.txt have same content.

-----
 Details for datamash:
datamash        command-line calculations (man)
Path    : /usr/bin/datamash
Version : 1.0.6
Type    : ELF 64-bit LSB executable, x86-64, version 1 (SYSV ...)
Help    : probably available with -h,--help
Repo    : Debian 8.7 (jessie) 
Home    : http://www.gnu.org/software/datamash

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Old 03-23-2017
Quote:
Originally Posted by mukulverma2408
I saw this problem on one of the forum and solved it using group-by in oracle sql, though I am a bit curious to implement it using shell script
I have actually written a whole article about exactly this:

How to do a "Control Break" (Algorithm)

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 7  
Old 03-23-2017
Thanks Every-one, that is really helpful Smilie

---------- Post updated at 05:55 PM ---------- Previous update was at 05:27 PM ----------

Quote:
Originally Posted by Scrutinizer
Hi, try:
Hi Scrutinizer, thanks for the solution, the problem is I am not very well versed with awk, would be really helpful if you can explain what's happening here.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script to Group by Based on Multiple Fields in a file

Hi, I want to know if there is any simple approach to SUM a field based on group by of different fields for e.g. file1.txt contains below data 20160622|XXX1||50.00||50.00|MONEY|Plan1| 20160622|XXX1||100.00||100.00|MONEY|Plan1| 20160623|XXX1||25.00||25.00|MONEY|Plan1|... (3 Replies)
Discussion started by: cnu_theprince
3 Replies

2. Shell Programming and Scripting

Shell script - group by

Hi, I have text file as shown below. root 25 oracle 25 batch 30 griduser 32 admin 35 root 25 oracle 25 batch 30 griduser 32 oracle 25 batch 30 griduser 32 xuser 45 admin 35 I want to group by based on user name, and the output need to be as below. Not necessary the username to be... (10 Replies)
Discussion started by: baladelaware73
10 Replies

3. Shell Programming and Scripting

Emulate fgrep -f in perl

Is there any equivalent of the below requirement in perl fgrep -f file1 file2 > file3 (2 Replies)
Discussion started by: aravindj80
2 Replies

4. Shell Programming and Scripting

Shell Script to ignore # and take corresponding user and group

Hi, I have a following file: role.IMPACT_USER.user=admin role.IMPACT_USER.user=dd12345 role.IMPACT_USER.user=ss76767 #role.IMPACT_USER.user=root #role.IMPACT_USER.group=System role.IMPACT_USER.group=ImpactUser #Description: Allow users to login in to Impact, start and stop service... (5 Replies)
Discussion started by: dbashyam
5 Replies

5. Shell Programming and Scripting

"group by" using shell script?

not sure if it's called "group by" , but what i'm going to do is like this: i have a file below: 192.168.1.10 192.168.1.10 192.168.1.10 192.168.1.11 192.168.1.15 192.168.1.15 192.168.1.20 192.168.1.22 then i hope to get the result like this: 192.168.1.10 : 3 192.168.1.11 : 1... (6 Replies)
Discussion started by: tiger2000
6 Replies

6. Shell Programming and Scripting

Shell script to rename a group of files

Hello, I am having 1800 files in a directory with a specified format, like amms_850o_prod.000003uNy amms_850o_prod.000003u8x amms_850o_prod.000003taP amms_850o_prod.000003tKy amms_850o_prod.000003si4 amms_850o_prod.000003sTP amms_850o_prod.000003sBg amms_850o_prod.000003rvx... (12 Replies)
Discussion started by: atlantis
12 Replies

7. Shell Programming and Scripting

How to emulate ^S/^Q from a script

Hi, I wrote a little menu script that searches through another script you specify and displays step-names and next to it the text of the step. The scripts are converted JCL from mainframe. It alows you to select steps you want and will then create a new script which includes only the steps you... (5 Replies)
Discussion started by: AliceD
5 Replies

8. Shell Programming and Scripting

HELP! Group by in shell script (awk/sed?)

Hello, Could some expert soul please help me with this? I have following file format - task time abc 5 xyz 4 abc 5 xyz 3 ddd 10 ddd 2 I need to generate output as - task ... (5 Replies)
Discussion started by: sncoupons
5 Replies

9. Shell Programming and Scripting

Shell quiz: emulate an associative array

Most shells flavors do not have associative arrays a.k.a. maps. How would you emulate an associative array? I had this problem once and found a working solution, but I don't want to spoil the game hence I wont tell it. Wonder if anyone comes up with something better. (5 Replies)
Discussion started by: colemar
5 Replies

10. Shell Programming and Scripting

Script to emulate ls -lh?

Does anyone have a script they would like to share that emulates "ls -lh" in ksh on Solaris 8? Yeah, I know. Real men don't need that wimpy "h." Well, I'm a wimp. ;) (0 Replies)
Discussion started by: shew01
0 Replies
Login or Register to Ask a Question