[英]awk: read file line by line and sed to parse IP from column
I have an issue using awk and sed to parse a log file. 我在使用awk和sed解析日志文件时遇到问题。
The below file: 下面的文件:
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
Using the following command works fine to strip out IP from column 7. I am relying on removing last 5 characters from end to output only IP. 使用以下命令可以很好地从第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`
output as expeted: 输出结果:
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
9 out of 10 times this is fine, but just found out column 7 will not always contain 4 digits at the end. 10次中有9次是可以的,但只是发现第7列并不总是在末尾包含4位数字。 Column 7 contains user input data so if something larger than 4 digit is entered incorrectly, it does not strip the IP as needed. 列7包含用户输入数据,因此如果输入的数字大于4位,则不会按需剥离IP。 How can I remove everything but the IP for that column? 如何删除该列的IP以外的所有内容?
Example of incorrect field: 错误字段示例:
DDDD;callID:3456;AAAA;Body:test message 3;BBBB;ng?10.200.1.5 3456345;failed
bad output: 输出不良:
3456;test message 3;failed;10.200.1.5 34
I was trying to use 我正在尝试使用
grep -Eo '\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b'
inside the {print.....} section but could not get it to work..... 在{print .....}部分中,但无法正常工作.....
thank you in advance for your help. 预先感谢您的帮助。
$ 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
Using awk 使用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.