Group words based on the value of the first column

Avinash Raj

I want to group all the file contents based on the value of first column. It would be better if the output is in sorted form.

Input:

1 foo
2 bar
1 foobar
2 barbar
3 apple
4 banana
3 mango
5 orange

Expected output:

1 foo, foobar
2 bar, barbar
3 apple, mango
4 banana
5 orange
terdon

If I understand correctly, you're looking for

perl -lane 'push @{$k{$F[0]}},$F[1]; 
            END{$"=", ";print "$_ @{$k{$_}}" for sort keys(%k)}' file

There's no reason to explicitly split the line, the -a flag already does it for you. It will split each input line into the @F array. So, here we create the %k hash whose keys are the 1st fields and whose values are the corresponding lists of 2nd fields. Then, at the end, we sort the hash keys, set the list separator ($") to ,[space] and print each key and its corresponding list of values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

‌How to select rows with minimum value in each group based on first column as ID?

From Dev

How to group by first 2 words in a pandas dataframe column and count?

From Dev

Delete a row based on the first value of the group

From Dev

Change first value of group based on condition, pandas

From Dev

Selecting the first and last value in a column in a group by query

From Dev

First letter of group of words

From Dev

Group by query based on other column value

From Dev

Assign value to group based on condition in column

From Dev

Group items in a table based on value in column and count

From Dev

mysql group records based on a column value

From Dev

Sum group of rows based on value in a column

From Dev

Group rows based on column sum value

From Dev

Get the first instance of a record based on the value of a column

From Dev

Update first header column based on checkbox value

From Dev

Return value pairs based on first row and column

From Dev

Remove entry based on the value of first column

From Dev

MySQL first select column based on parameter value and then value of column

From Dev

MySQL first select column based on parameter value and then value of column

From Java

Cumulatively add month to a Date column based off the first date of a group

From Dev

SQL Subtract Column Values Based on Second Column Value with Group By Statement

From Dev

Mysql query for sum based off a value in a column group by that column

From Dev

Mysql query for sum based off a value in a column group by that column

From Dev

Repeat the value first column based on key column - Linux

From Dev

Repeat the value first column based on key column - Linux

From Dev

get all first occurences of a column based on the value of another column - matlab

From Dev

Add value based on column value and group in SQL query.

From Dev

How to subset datatable based on (value of first row + X), by group

From Dev

Using mutate to create a new column with the first value of each group in R

From Dev

Generateing a per-group sequence based on a specific value in an existing column

Related Related

  1. 1

    ‌How to select rows with minimum value in each group based on first column as ID?

  2. 2

    How to group by first 2 words in a pandas dataframe column and count?

  3. 3

    Delete a row based on the first value of the group

  4. 4

    Change first value of group based on condition, pandas

  5. 5

    Selecting the first and last value in a column in a group by query

  6. 6

    First letter of group of words

  7. 7

    Group by query based on other column value

  8. 8

    Assign value to group based on condition in column

  9. 9

    Group items in a table based on value in column and count

  10. 10

    mysql group records based on a column value

  11. 11

    Sum group of rows based on value in a column

  12. 12

    Group rows based on column sum value

  13. 13

    Get the first instance of a record based on the value of a column

  14. 14

    Update first header column based on checkbox value

  15. 15

    Return value pairs based on first row and column

  16. 16

    Remove entry based on the value of first column

  17. 17

    MySQL first select column based on parameter value and then value of column

  18. 18

    MySQL first select column based on parameter value and then value of column

  19. 19

    Cumulatively add month to a Date column based off the first date of a group

  20. 20

    SQL Subtract Column Values Based on Second Column Value with Group By Statement

  21. 21

    Mysql query for sum based off a value in a column group by that column

  22. 22

    Mysql query for sum based off a value in a column group by that column

  23. 23

    Repeat the value first column based on key column - Linux

  24. 24

    Repeat the value first column based on key column - Linux

  25. 25

    get all first occurences of a column based on the value of another column - matlab

  26. 26

    Add value based on column value and group in SQL query.

  27. 27

    How to subset datatable based on (value of first row + X), by group

  28. 28

    Using mutate to create a new column with the first value of each group in R

  29. 29

    Generateing a per-group sequence based on a specific value in an existing column

HotTag

Archive