kwaldner
kwaldner

Reputation: 95

Reformatting rows

I have several rows that look something like this,

[206, 40, 200] 3.588939213064659e-06
[206, 40, 58, 200] 1.0137134449402395e-05
[206, 200] 1.2268187790002155e-05
[206, 38, 200] 3.2630262623982875e-05
[206, 40, 38, 200] 3.273281423485983e-05
[206, 40, 58, 38, 200] 3.341643719910475e-05

Is there an efficient way I could convert them to the following using sed/awk,

206 40 200
206 40 58 200
206 200
206 38 200
206 40 38 200 
206 40 58 38 200

and this,

206 40 200 0.000003588939213064659
206 40 58 200 0.000010137134449402395
206 200 0.000012268187790002155
206 38 200 0.000032630262623982875
206 40 38 200 0.00003273281423485983
206 40 58 38 200 0.00003341643719910475

Thanks!

Upvotes: 1

Views: 40

Answers (3)

vintnes
vintnes

Reputation: 2030

You just need a regex Field Separator and format conversion to variable.

$ awk -F'[][, ]+' '$NF=sprintf("%.21f",$NF)' file

edit: The easy one is awk -F'[][, ]+' '{$NF=""}1'

Upvotes: 1

tshiono
tshiono

Reputation: 22012

Answering the latter requirement, please try:

awk '
{
    split($0, a, "] +")
    gsub("[[,]", "", a[1])
    printf("%s %.21f\n", a[1], a[2])
}' file

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133428

For your first question try following.

awk 'match($0,/\[.*\]/){val=substr($0,RSTART+1,RLENGTH-2);gsub(","," ",val);print val}'  Input_file

For your second question could you please try following.

awk 'match($0,/\[.*\]/){val=substr($0,RSTART+1,RLENGTH-2);gsub(","," ",val);printf("%s %.21f\n",val,$NF)}'  Input_file

Upvotes: 0

Related Questions