我如何像这样转换表格:
1 0.75 1 0.38
2 0.80 2 0.18
Run Code Online (Sandbox Code Playgroud)
类似于:
1 0.75
2 0.80
1 0.38
2 0.18
Run Code Online (Sandbox Code Playgroud)
coloumn由制表符或逗号分隔.
我想在linux bash中使用任意数量的colounm和rows.我尝试用awk做但没找到正确的方法.
谢谢
这是 awk 中的一个:
$ awk '
BEGIN{ FS=OFS="\t" } # tab-delimited
NR==1{ nf=NF } # get field count
{
for(i=1;i<nf;i+=2) # iterate every other field
if(i==1) # print first 2 fields
print $1,$2
else # buffer others
b[i]=b[i] sprintf("%s" OFS "%s" ORS,$i,$(i+1))
}
END { # in the end
for(i=3;i<nf;i+=2) # iterate buffers
printf "%s",b[i] # and output
}' file
1 0.75
2 0.80
1 0.38
2 0.18
Run Code Online (Sandbox Code Playgroud)
编辑: awk-sort-cut 混合体:
$ awk ' # awk to add field number to output
BEGIN { FS=OFS="\t" } # tab-delimiter
{
for(i=1;i<NF;i+=2) # iterate fields
print i,$i,$(i+1) # print field number and data fields
}' file |
sort -s -t $'\t' -k1n | # sort on the first field only
cut -f 2-
1 0.75
2 0.80
1 0.38
2 0.18
Run Code Online (Sandbox Code Playgroud)