[英]How to combine awk and sed in while read line from text file to pull parts and rearrange the output
[英]awk: read file line by line and sed to parse IP from column
我在使用awk和sed解析日志文件時遇到問題。
下面的文件:
DDDD;callID:4565;AAAA;Body:test message 1;BBBB;ng?10.1.5.60 4565;complete
DDDD;callID:5489;AAAA;Body:test message 2;BBBB;ng?10.4.100.3 5489;complete
DDDD;callID:3456;AAAA;Body:test message 3;BBBB;ng?10.200.1.5 3456;failed
使用以下命令可以很好地從第7列中刪除IP。我依靠從末尾刪除最后5個字符來僅輸出IP。
`awk -F";" ' {print $2 ";" $4 ";" $7 ";" $6}' test.CSV | sed -e 's/Body://g; s/callID://g; s/ng?//g' | sed 's/.\{5\}$//' >> complete.txt`
輸出結果:
4565;test message 1;complete;10.1.5.60
5489;test message 2;complete;10.4.100.3
3456;test message 3;failed;10.200.1.5
10次中有9次是可以的,但只是發現第7列並不總是在末尾包含4位數字。 列7包含用戶輸入數據,因此如果輸入的數字大於4位,則不會按需剝離IP。 如何刪除該列的IP以外的所有內容?
錯誤字段示例:
DDDD;callID:3456;AAAA;Body:test message 3;BBBB;ng?10.200.1.5 3456345;failed
輸出不良:
3456;test message 3;failed;10.200.1.5 34
我正在嘗試使用
grep -Eo '\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b'
在{print .....}部分中,但無法正常工作.....
預先感謝您的幫助。
$ awk -F'[:;?]' -v OFS=';' '{sub(/ .*/,"",$9); print $3,$6,$10,$9}' file
4565;test message 1;complete;10.1.5.60
5489;test message 2;complete;10.4.100.3
3456;test message 3;failed;10.200.1.5
使用awk
awk '{split($2,a,":");split($4,b,":");split($6,c,"[? ]");print a[2],b[2],$7,c[2]}' FS=";" OFS=";" file
4565;test message 1;complete;10.1.5.60
5489;test message 2;complete;10.4.100.3
3456;test message 3;failed;10.200.1.5
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.