我的文本文件(reference.txt)中的数据分布分为三个部分。每一节都以单词"ABH“开头的行作分界。每个部分由许多行组成。我的动机是将每一节中的许多行转换为单个列。最后,每个部分应该有三列。
我的参考数据文件
-317 -324 -303 -315 -319 -319 01
-310 -326 -331 -322 -327 -307 02
-330 -316 -317 -323 -318 -321 03
-320 -315 -307 -326 -341 -307 04
-323 -327 -333 -330 -303 -304 05
-308 -322
ABH R N148 18 1 18 6 44 40.744 980.00 757 4
-304 -300 -308 -307 -324 -329 -325
-321 -304 -302 -309 -320 -317 -296
-304 -330 -328 -298 -283 -312 -335
-330 -324 -312 -298 -301 -321 -326
-297 -287 -317 -336 -324 -297 -288
-301 -321
ABH R E148 18 1 18 6 44 40.744 980.00 757 4
79 87 117 103 81 92 97
88 96 103 97 99 105 99
94 98 95 90 89 107 99
65 107 143 87 62 99 112
85 74 97 118 115 95 98
97 81我的最终输出应该如下所示
-317 -304 79
-324 -300 87
-303 -308 117
-315 -307 103
-319 -324 81
-319 -329 92
-01 -325 97
-310 -321 88
-326 -304 96
-331 -302 103
-322 -309 97
-327 -320 99
-307 -317 105
-02 -296 99
-330 -304 94
-316 -330 98
-317 -328 95
-323 -298 90
-318 -283 89
-321 -312 107
-03 -335 99
-320 -330 65
-315 -324 107
-307 -312 143
-326 -298 87
-341 -301 62
-307 -321 99
-04 -326 112
-323 -297 85
-327 -287 74
-333 -317 97
-330 -336 118
-303 -324 115
-304 -297 95
-05 -288 98
-308 -301 97
-322 -321 81我可以使用shell脚本(如下所示)排列一个部分中的数据,但同时如何将其应用于其他两个部分,并将所有列放在一起,如上所示。我使用的单节脚本如下所示。如果有人能帮上忙,那将是非常有帮助的。谢谢。预先。
for num in $(cat reference.txt)
do
echo $num >>output.txt
done发布于 2021-02-15 22:07:34
使用GNU awk:
awk 'BEGIN { cnt=1 } /^ABH/ { cnt1=0;cnt++;next;cnt1=0 } { for(i=1;i<=NF;i++) { cnt1++;map[cnt][cnt1]=$i } } END { PROCINFO["sorted_in"]="@ind_num_asc";for(i in map[1]) { for (j=1;j<=cnt;j++) { printf "%s\t",map[j][i] } printf "\n" } }' file解释:
awk 'BEGIN {
cnt=1 # Initialise a ABH record variable before file processing
}
/^ABH/ { cnt1=0; # When we see "ABH", increment cnt and set cnt1 to 0
cnt++;
next;
}
{
for(i=1;i<=NF;i++) {
cnt1++; # Loop through each spaces delimited field and increment cnt1
map[cnt][cnt1]=$i # Add to a 2 dimensional array with cnt the first index and cnt1 the second. The field value the value.
}
}
END { # Process at the end of the file
PROCINFO["sorted_in"]="@ind_num_asc"; # Set the array ordering
for(i in map[1]) {
for (j=1;j<=cnt;j++) {
printf "%s\t",map[j][i] # Loop through each index of the array printing the values.
}
printf "\n"
}
}' filehttps://stackoverflow.com/questions/66208267
复制相似问题