Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two parts of data?

Tags:

linux

awk

The redis data in the demo file aaa is:

million:relive:2270371,18,1
million:relive:user:49976159,27:8,340|2018-01-26 20:25:00
million:relive:stage:22,6,1

The result is to add an additional column based on the thrid part of the redis key :

uid,million:relive:2270371,18,1
user,million:relive:user:49976159,27:8,340
stage,million:relive:stage:22,6,1

I can generate the first column using:

awk -F":" '{print $3}' aaa | awk '{if($0 ~ /^[0-9,]+?$/) print "uid"; else print $0}'

And the second part:

awk -F"|" '{print $1}' aaa

How to combine the two parts and is it possible to make them in one sentence?

like image 862
SuperDelta Avatar asked Mar 07 '23 00:03

SuperDelta


1 Answers

$ awk -F':' '{sub(/\|.*/,"");print ($3~/^[0-9]+,/?"uid":$3)","$0}' aaa
uid,million:relive:2270371,18,1
user,million:relive:user:49976159,27:8,340
stage,million:relive:stage:22,6,1

The above one-liner should help you. It first removed the |...... part, then add the key depending on the $3

like image 78
Kent Avatar answered Mar 16 '23 09:03

Kent