![如何将文本文件格式化为所需的输出](https://linux22.com/image/94188/%E5%A6%82%E4%BD%95%E5%B0%86%E6%96%87%E6%9C%AC%E6%96%87%E4%BB%B6%E6%A0%BC%E5%BC%8F%E5%8C%96%E4%B8%BA%E6%89%80%E9%9C%80%E7%9A%84%E8%BE%93%E5%87%BA.png)
文本文件:
***** Connected to QBBSC01 *****
MO
RXOTG-0
DEV DCP APUSAGE APSTATE 64K TEI
RBLT2-161 1 UNCONC SPEECH/DATA YES
RBLT2-162 2 UNDEF IDLE YES
RBLT2-163 3 UNCONC SPEECH/DATA YES
RBLT2-164 4 UNDEF IDLE YES
***** Disconnected from QBBSC01 *****
***** Connected to QBBSC04 *****
MO
RXOTG-23
DEV DCP APUSAGE APSTATE 64K TEI
RBLT2-161 1 UNCONC SPEECH/DATA YES
RBLT2-162 2 UNDEF IDLE YES
RBLT2-163 3 UNCONC SPEECH/DATA YES
RBLT2-164 4 UNDEF IDLE YES
RBLT2-165 5 UNCONC SPEECH/DATA YES
RBLT2-166 6 UNCONC SPEECH/DATA YES
RBLT2-167 7 UNDEF IDLE NO
RBLT2-168 8 UNCONC SPEECH/DATA NO
***** Disconnected from QBBSC04 *****
***** Connected to QBBSC05 *****
MO
RXOTG-7
DEV DCP APUSAGE APSTATE 64K TEI
RBLT2-161 1 UNCONC SPEECH/DATA YES
RBLT2-162 2 UNDEF IDLE YES
RBLT2-163 3 UNCONC SPEECH/DATA YES
RBLT2-164 4 UNDEF IDLE YES
RBLT2-165 5 UNCONC SPEECH/DATA YES
RBLT2-166 6 UNCONC SPEECH/DATA YES
***** Disconnected from QBBSC05 *****
期望的输出:
NE MO DEV DCP APUSAGE APSTATE 64K TEI
QBBSC01 RXOTG-0 RBLT2-161 1 UNCONC SPEECH/DATA YES
QBBSC01 RXOTG-0 RBLT2-162 2 UNDEF IDLE YES
QBBSC01 RXOTG-0 RBLT2-163 3 UNCONC SPEECH/DATA YES
QBBSC01 RXOTG-0 RBLT2-164 4 UNDEF IDLE YES
NE MO DEV DCP APUSAGE APSTATE 64K TEI
QBBSC04 RXOTG-23 RBLT2-161 1 UNCONC SPEECH/DATA YES
QBBSC04 RXOTG-23 RBLT2-162 2 UNDEF IDLE YES
QBBSC04 RXOTG-23 RBLT2-163 3 UNCONC SPEECH/DATA YES
QBBSC04 RXOTG-23 RBLT2-164 4 UNDEF IDLE YES
QBBSC04 RXOTG-23 RBLT2-165 5 UNCONC SPEECH/DATA YES
QBBSC04 RXOTG-23 RBLT2-166 6 UNCONC SPEECH/DATA YES
QBBSC04 RXOTG-23 RBLT2-167 7 UNDEF IDLE NO
QBBSC04 RXOTG-23 RBLT2-168 8 UNCONC SPEECH/DATA NO
NE MO DEV DCP APUSAGE APSTATE 64K TEI
QBBSCO5 RXOTG-7 RBLT2-161 1 UNCONC SPEECH/DATA YES
QBBSCO5 RXOTG-7 RBLT2-162 2 UNDEF IDLE YES
QBBSCO5 RXOTG-7 RBLT2-163 3 UNCONC SPEECH/DATA YES
QBBSCO5 RXOTG-7 RBLT2-164 4 UNDEF IDLE YES
QBBSCO5 RXOTG-7 RBLT2-165 5 UNCONC SPEECH/DATA YES
QBBSCO5 RXOTG-7 RBLT2-166 6 UNCONC SPEECH/DATA YES
请不要考虑行号。
答案1
从问题中尚不清楚各个领域可能有何不同。但对于您的示例,一个简单的awk
程序可能如下所示:
/Connected/ { NE=$4 }
/RXOTG/ { MO=$1 }
/DEV/ { printf "\n\nNE %*s %s\n", -length(MO), "MO", $0 }
/RBLT2/ { print NE, MO, $0 }