使用乱序和无序数据创建 table
Creating a table with scrambled and unordered data
我正在尝试创建一个不错的 table 值,我正在使用 bash 来创建它,但并非所有值都是有序的。不仅这些值也恰好在它们自己的文件中。我最初的几个想法是使用 cat 和 grep 来获取值,但从那里我不确定什么是合适的。我觉得 awk 在这种情况下会创造奇迹,但我对 awk 不是很了解。
file1 可能看起来像这样
V 0.001
A 98.6
N Measurement1
T 14:15:01
S 20.2
F 212.86
G 28.19
file2 可能看起来像这样
V 0.008
A 103.4
N Measurement2
T 16:20:31
S 21.2
F 215.86
G 28.19
最终文件如下所示
N Measurement1 Measurement2
T 14:15:01 16:20:31
V 0.001 0.008
G 28.19 28.19
A 98.6 103.4
S 20.2 21.2
F 212.86 215.86
自己评论,提供代码理解awk
awk '
# cretate new reference (per file)
FNR==1{Ref++}
# each line
{ # add label to memory
N[]
# add value in 2 dimension array
V[Ref ":" ] =
# remember maximum length of this serie
if( length( ) > M[Ref] ) M[Ref] = length( )
}
# after last file
END{
# print header (name of the serie)
printf( "N ")
for( i=1;i<=Ref;i++) printf( "%" M[i] "s ", V[ i ":N" ] )
printf( "\n")
# print each data for this label (format suite the size to be aligned)
# don t print a second time the name of the serie
for ( n in N ){
if( n != "N" ){
printf( "%s ", n)
for( i=1;i<=Ref;i++) printf( "%" M[i] "s ", V[ i ":" n ] )
printf( "\n")
}
}
}
' file*
我正在尝试创建一个不错的 table 值,我正在使用 bash 来创建它,但并非所有值都是有序的。不仅这些值也恰好在它们自己的文件中。我最初的几个想法是使用 cat 和 grep 来获取值,但从那里我不确定什么是合适的。我觉得 awk 在这种情况下会创造奇迹,但我对 awk 不是很了解。
file1 可能看起来像这样
V 0.001
A 98.6
N Measurement1
T 14:15:01
S 20.2
F 212.86
G 28.19
file2 可能看起来像这样
V 0.008
A 103.4
N Measurement2
T 16:20:31
S 21.2
F 215.86
G 28.19
最终文件如下所示
N Measurement1 Measurement2
T 14:15:01 16:20:31
V 0.001 0.008
G 28.19 28.19
A 98.6 103.4
S 20.2 21.2
F 212.86 215.86
自己评论,提供代码理解awk
awk '
# cretate new reference (per file)
FNR==1{Ref++}
# each line
{ # add label to memory
N[]
# add value in 2 dimension array
V[Ref ":" ] =
# remember maximum length of this serie
if( length( ) > M[Ref] ) M[Ref] = length( )
}
# after last file
END{
# print header (name of the serie)
printf( "N ")
for( i=1;i<=Ref;i++) printf( "%" M[i] "s ", V[ i ":N" ] )
printf( "\n")
# print each data for this label (format suite the size to be aligned)
# don t print a second time the name of the serie
for ( n in N ){
if( n != "N" ){
printf( "%s ", n)
for( i=1;i<=Ref;i++) printf( "%" M[i] "s ", V[ i ":" n ] )
printf( "\n")
}
}
}
' file*