我正在处理这条多行换位 输入并转置到
nickname jeff site A site_out out 9C:02:24
nickname jeff site A site_in in 9C:02:24
nickname jeff site A in DOOR_SECT_X1 68:01:40
nickname jeff site A in DOOR_SECT_A1 68:01:10
nickname david site B site_in in EA:A7:D3
nickname david site B in EXT_MAT-G 80:16:7B
nickname martin site C in EXT_MAT-H 80:16:7B
nickname martin site C in ARR_N1D 68:01:10
nickname martin site C in ARR_N2D 68:01:10
nickname geoff site A site_out out 00:C9:AB
nickname geoff site A site_in in 00:C9:AB
nickname geoff site A in DEF_DOOR_ARR 80:07:E6
此输出
nickname jeff site A
site_out out 9C:02:24
site_in in 9C:02:24
in DOOR_SECT_X1 68:01:40
in DOOR_SECT_A1 68:01:10
nickname david site B
site_in in EA:A7:D3
in EXT_MAT-G 80:16:7B
nickname martin site C
in EXT_MAT-H 80:16:7B
in ARR_N1D 68:01:10
in ARR_N2D 68:01:10
nickname geoff site A
site_out out 00:C9:AB
site_in in 00:C9:AB
in DEF_DOOR_ARR 80:07:E6
其中重复的“昵称”以及第二列,例如马丁/杰夫删除并仅保留一次,并且在第4列完成后,跳到下一行
我被卡在了这里,下面的代码返回了
awk '{k=$1 FS $2 FS$3 FS$4 FS; a[k]=a[k] FS $NF}
END {for(k in a) print k "\n" a[k]}'
nickname david site B
EA:A7:D3 80:16:7B
nickname geoff site A
00:C9:AB 00:C9:AB 80:07:E6
nickname martin site C
80:16:7B 68:01:10 68:01:10
nickname jeff site A
9C:02:24 9C:02:24 68:01:40 68:01:10
感谢您提示如何进行此操作
答案 0 :(得分:2)
此脚本取得了一些成功:
您需要知道何时停止打印连续的昵称:这是$2 != nickname
的情况。如果是这样,它将打印昵称和其余的列(即print_data
函数),并“跳到”文件的下一行(即next
)
print_data
函数在昵称部分之后打印所有剩余的列。如果您在该行的末尾添加更多列,则无需更改该脚本。
BEGIN {
nickname = ""
}
function print_data() {
for (i=5; i<=NF; i++) {
printf "%s ", $i
}
print ""
}
# change "$2" if you add more column before nickname
$2 != nickname {
nickname = $2
print $1, $2, $3, $4
print_data()
next
}
{
print_data()
}
答案 1 :(得分:0)
查看此Perl单线版
$ cat ya801.txt
nickname jeff site A site_out out 9C:02:24
nickname jeff site A site_in in 9C:02:24
nickname jeff site A in DOOR_SECT_X1 68:01:40
nickname jeff site A in DOOR_SECT_A1 68:01:10
nickname david site B site_in in EA:A7:D3
nickname david site B in EXT_MAT-G 80:16:7B
nickname martin site C in EXT_MAT-H 80:16:7B
nickname martin site C in ARR_N1D 68:01:10
nickname martin site C in ARR_N2D 68:01:10
nickname geoff site A site_out out 00:C9:AB
nickname geoff site A site_in in 00:C9:AB
nickname geoff site A in DEF_DOOR_ARR 80:07:E6
$ perl -lane ' $y=join(" ",@F[0..3]) ; $z=join(" ",@F[4..$#F]); if ($x ne $y) { print "$y\n$z";$x=$y } else { print "$z";} ' ya801.txt
nickname jeff site A
site_out out 9C:02:24
site_in in 9C:02:24
in DOOR_SECT_X1 68:01:40
in DOOR_SECT_A1 68:01:10
nickname david site B
site_in in EA:A7:D3
in EXT_MAT-G 80:16:7B
nickname martin site C
in EXT_MAT-H 80:16:7B
in ARR_N1D 68:01:10
in ARR_N2D 68:01:10
nickname geoff site A
site_out out 00:C9:AB
site_in in 00:C9:AB
in DEF_DOOR_ARR 80:07:E6
$