user3369871 - 10 months ago 110

Bash Question

I have a file which contains task name in the first column and the time taken to complete the task in the second column as below :

`Task2, 3421`

Task3, 3300

Task1, 1000

Task2, 1100

Task3, 1200

Task3, 1209

Task4, 1299

Task3, 1289

Task1, 1389

Task2, 1211

Task5, 1216

Task2, 1416

Task1, 2100

Task6, 2416

Task5, 2216

Task7, 1116

Now I have to find the min and max time taken for each task and output in below format

`task , maxtime , min time`

e.g.

`Task1, 1000, 2100 ( from the data given above)`

Answer Source

Another way to do it is to sort by column1 and then by column2 and take 1st and last value for each task like this

```
awk -F, '{arr[$1]=arr[$1] $2} END {for(key in arr) print key, arr[key]}' <(sort -t 1 -k 1,2 file) | awk '{OFS=", "; print $1, $2, $NF}'
```

Sample run:

```
$ cat file
Task2, 3421
Task3, 3300
Task1, 1000
Task2, 1100
Task3, 1200
Task3, 1209
Task4, 1299
Task3, 1289
Task1, 1389
Task2, 1211
Task5, 1216
Task2, 1416
Task1, 2100
Task6, 2416
Task5, 2216
Task7, 1116
$ sort -t 1 -k 1,2 file
Task1, 1000
Task1, 1389
Task1, 2100
Task2, 1100
Task2, 1211
Task2, 1416
Task2, 3421
Task3, 1200
Task3, 1209
Task3, 1289
Task3, 3300
Task4, 1299
Task5, 1216
Task5, 2216
Task6, 2416
Task7, 1116
$ awk -F, '{arr[$1]=arr[$1] $2} END {for(key in arr) print key, arr[key]}' <(sort -t 1 -k 1,2 file) | awk '{OFS=", "; print $1, $2, $NF}'
Task1, 1000, 2100
Task2, 1100, 3421
Task3, 1200, 3300
Task4, 1299, 1299
Task5, 1216, 2216
Task6, 2416, 2416
Task7, 1116, 1116
```