[英]Formatting text output in Bash
我有一些來自pdsh的輸出,我想將其格式化以加載到數據庫中。 我在3列中需要它,本質上是要在一行上顯示模塊ID和消息。 波紋管是我的輸出:
10.125.45.58,scope, Module ID = server-1
10.125.45.58,scope, Message = Correctable memory error
10.125.45.58,scope, Module ID = server-2
10.125.45.58,scope, Message = Correctable memory error
這是我需要的輸出:
10.125.45.58,scope,server-1 Correctable memory error
10.125.45.58,scope,server-2 Correctable memory error
我一直在清醒和鎮靜...我無法解決任何想法?
這是我到目前為止所做的
cat myfile.txt | sed -e "s/:/\,scope\,/g" | grep -E '(Module ID|Message)'
謝謝。
$ cat ip.txt
10.125.45.58,scope, Module ID = server-1
10.125.45.58,scope, Message = Correctable memory error
10.125.45.58,scope, Module ID = server-2
10.125.45.58,scope, Message = Correctable memory error
$ sed 'N; s/\n.*=//; s/ *Module ID.*= *//' ip.txt
10.125.45.58,scope,server-1 Correctable memory error
10.125.45.58,scope,server-2 Correctable memory error
N
獲得下一行,因此替換適用於同時考慮的每兩行 s/\\n.*=//;
從換行符刪除到last =
s/ *Module ID.*= *//
然后從Module ID
刪除所有內容,直到=
為止,並在周圍加上可選空格 sed用於單行上的簡單替換,僅此而已。 對於其他任何事情,您都應該使用awk來實現軟件的可移植性,效率,簡單性,清晰度,可維護性以及大多數其他所需的屬性:
$ awk 'NR%2{srvr=$NF;next} {ip=$1; sub(/.*=/,""); print ip srvr $0}' file
10.125.45.58,scope,server-1 Correctable memory error
10.125.45.58,scope,server-2 Correctable memory error
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.