awk 根据模式转置行并移动(复制)之后的剩余列
awk transpose lines based on pattern and move (copy) remaining columns after
如何解决强制模式为“刻痕”的问题,基于此将其转换为列和
模式“MY_”或“MN_”旁边有“MX_”(MX_ 模式应位于输入文件中每个“NICK”部分下的任何位置)
试过这个
但它限制了我想要实现的目标
输入文件:
NICK: MY_JEFF
MX_NOAA; NOBASE-INDEX_OUT_FIF;
SYS-BRAVO_X3; BASE-TREE_OUT_OUT;
A4:82:8; SEAT_25;
SYS-BRAVO_X2
NICK: MN_CAPTAIN
A4:82:8; NOBASE-INDEX_OUT_FIF;
SYS-BRAVO_X1; BASE-TREE_OUT_OUT;
MX_NOOI
期望的输出:
NICK MY_JEFF MX_NOAA NOBASE-INDEX_OUT_FIF
NICK MY_JEFF MX_NOAA SYS-BRAVO_X3
NICK MY_JEFF MX_NOAA BASE-TREE_OUT_OUT
NICK MY_JEFF MX_NOAA A4:82:8
NICK MY_JEFF MX_NOAA SEAT_25
NICK MY_JEFF MX_NOAA SYS-BRAVO_X2
NICK MN_CAPTAIN MX_NOOI A4:82:8
NICK MN_CAPTAIN MX_NOOI NOBASE-INDEX_OUT_FIF
NICK MN_CAPTAIN MX_NOOI SYS-BRAVO_X1
NICK MN_CAPTAIN MX_NOOI BASE-TREE_OUT_OUT
$ cat tst.awk
BEGIN { FS=": |([; ]+)" }
== "NICK" { prt() }
{
for (i=1; i<=NF; i++) {
rec[++nf] = $i
}
}
END { prt() }
function prt( i, hdr) {
for (i=3; i<=nf; i++) {
if ( rec[i] ~ /^MX/ ) {
break
}
}
hdr = rec[1] OFS rec[2] OFS rec[i]
delete rec[i]
for (i=3; i<=nf; i++) {
if ( rec[i] ~ /[^[:space:]]/ ) {
print hdr, rec[i]
}
}
delete rec
nf = 0
}
$ awk -f tst.awk file
NICK MY_JEFF MX_NOAA NOBASE-INDEX_OUT_FIF
NICK MY_JEFF MX_NOAA SYS-BRAVO_X3
NICK MY_JEFF MX_NOAA BASE-TREE_OUT_OUT
NICK MY_JEFF MX_NOAA A4:82:8
NICK MY_JEFF MX_NOAA SEAT_25
NICK MY_JEFF MX_NOAA SYS-BRAVO_X2
NICK MN_CAPTAIN MX_NOOI A4:82:8
NICK MN_CAPTAIN MX_NOOI NOBASE-INDEX_OUT_FIF
NICK MN_CAPTAIN MX_NOOI SYS-BRAVO_X1
NICK MN_CAPTAIN MX_NOOI BASE-TREE_OUT_OUT
如何解决强制模式为“刻痕”的问题,基于此将其转换为列和 模式“MY_”或“MN_”旁边有“MX_”(MX_ 模式应位于输入文件中每个“NICK”部分下的任何位置)
试过这个
输入文件:
NICK: MY_JEFF
MX_NOAA; NOBASE-INDEX_OUT_FIF;
SYS-BRAVO_X3; BASE-TREE_OUT_OUT;
A4:82:8; SEAT_25;
SYS-BRAVO_X2
NICK: MN_CAPTAIN
A4:82:8; NOBASE-INDEX_OUT_FIF;
SYS-BRAVO_X1; BASE-TREE_OUT_OUT;
MX_NOOI
期望的输出:
NICK MY_JEFF MX_NOAA NOBASE-INDEX_OUT_FIF
NICK MY_JEFF MX_NOAA SYS-BRAVO_X3
NICK MY_JEFF MX_NOAA BASE-TREE_OUT_OUT
NICK MY_JEFF MX_NOAA A4:82:8
NICK MY_JEFF MX_NOAA SEAT_25
NICK MY_JEFF MX_NOAA SYS-BRAVO_X2
NICK MN_CAPTAIN MX_NOOI A4:82:8
NICK MN_CAPTAIN MX_NOOI NOBASE-INDEX_OUT_FIF
NICK MN_CAPTAIN MX_NOOI SYS-BRAVO_X1
NICK MN_CAPTAIN MX_NOOI BASE-TREE_OUT_OUT
$ cat tst.awk
BEGIN { FS=": |([; ]+)" }
== "NICK" { prt() }
{
for (i=1; i<=NF; i++) {
rec[++nf] = $i
}
}
END { prt() }
function prt( i, hdr) {
for (i=3; i<=nf; i++) {
if ( rec[i] ~ /^MX/ ) {
break
}
}
hdr = rec[1] OFS rec[2] OFS rec[i]
delete rec[i]
for (i=3; i<=nf; i++) {
if ( rec[i] ~ /[^[:space:]]/ ) {
print hdr, rec[i]
}
}
delete rec
nf = 0
}
$ awk -f tst.awk file
NICK MY_JEFF MX_NOAA NOBASE-INDEX_OUT_FIF
NICK MY_JEFF MX_NOAA SYS-BRAVO_X3
NICK MY_JEFF MX_NOAA BASE-TREE_OUT_OUT
NICK MY_JEFF MX_NOAA A4:82:8
NICK MY_JEFF MX_NOAA SEAT_25
NICK MY_JEFF MX_NOAA SYS-BRAVO_X2
NICK MN_CAPTAIN MX_NOOI A4:82:8
NICK MN_CAPTAIN MX_NOOI NOBASE-INDEX_OUT_FIF
NICK MN_CAPTAIN MX_NOOI SYS-BRAVO_X1
NICK MN_CAPTAIN MX_NOOI BASE-TREE_OUT_OUT