SuperDelta
SuperDelta

Reputation: 253

How to merge two parts of data?

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?

Upvotes: 4

Views: 101

Answers (3)

sjsam
sjsam

Reputation: 21965

Below is another awk one-liner in your solution repertoire

$ awk -v OFS=":" '{sub(/\|.*$/,"");FS=":";sub(/^/,match($3,/^(user|stage)$/,a)?a[1]",":"uid,",$1)}1' file

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

Upvotes: 2

Allan
Allan

Reputation: 12456

You can use the following awk command:

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

This produce the following output:

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

Upvotes: 2

Kent
Kent

Reputation: 195239

$ 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

Upvotes: 3

Related Questions