Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awk with duplicate values

File:

22 Hello
22 Hi
1  What
34 Where
21 is
44 How
44 are
44 you

Desired Output:

22 HelloHi
1  What
34 Where
21 is
44 Howareyou

If there are duplicate values in first field($1) the second field should have appended text

How to achieve this using awk?

Thanks

like image 559
user1502952 Avatar asked Aug 14 '26 15:08

user1502952


1 Answers

$ awk '
!seen[$1]++ { keys[++numKeys] = $1 } 
{ str[$1] = str[$1] $2 }
END{
    for (keyNr=1; keyNr<=numKeys; keyNr++) {
        key = keys[keyNr]
        print key, str[key]
    }
}
' file
22 HelloHi
1 What
34 Where
21 is
44 Howareyou
like image 129
Ed Morton Avatar answered Aug 16 '26 07:08

Ed Morton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!