I have a log file in which we are getting ip address at wrong place.
mRedRect = new RectF(0, 0, 50, 50);
canvas.drawRect(mRedRect, mRedRectPaint);
mRedRect.offset(50, 0);
canvas.drawRect(mRedRect, mRedRectPaint);
if you see my above script it has ip address twice which is wrong in my case. I want to write a script that deletes 2nd id address.
I am thinking of getting cat test.sh
173.36.31.10 dasdsafafa fafsafaasfa fafasfaa173.36.31.10
173.36.31.11 dasdsafafa fafsafaasfa fafasfaa173.36.31.11
ip address by doing this but I am not sure how to delete the second ipaddress.
I want to do it in both shell and python, can someone help me?
I appreciate your help.
答案 0 :(得分:4)
使用它作为测试文件:
$ cat test.sh
173.36.31.10 dasdsafafa fafsafaasfa fafasfaa173.36.31.10
173.36.31.11 dasdsafafa fafsafaasfa fafasfaa173.36.31.11
这是从行的末尾删除第二次出现的IP的一种方法。
$ awk '{sub($1"$", "")} 1' test.sh
173.36.31.10 dasdsafafa fafsafaasfa fafasfaa
173.36.31.11 dasdsafafa fafsafaasfa fafasfaa
在awk中,$1
指定该行的第一个字段。因此,如果第一个字段在该行的末尾重复,sub($1"$", "")
将删除它。
更详细地说,对于正则表达式,$
表示行尾。因此$1"$"
将匹配行尾的第一个字段的重新出现。命令sub($1"$", "")
用空字符串替换这种重新出现。
$ while read ip rest; do echo "$ip ${rest%$ip}"; done <test.sh
173.36.31.10 dasdsafafa fafsafaasfa fafasfaa
173.36.31.11 dasdsafafa fafsafaasfa fafasfaa
这将查找该行末尾重复该行第一个单词的任何行。如果是,则删除重复:
$ sed -r 's/([^ ]*)( .*)\1$/\1\2/' test.sh
173.36.31.10 dasdsafafa fafsafaasfa fafasfaa
173.36.31.11 dasdsafafa fafsafaasfa fafasfaa
with open('test.sh') as fhandle:
for line in fhandle:
line = line.rstrip()
ip=line.split()[0]
if line.endswith(ip):
line=line[:-len(ip)]
print(line)
这会产生相同的输出。
答案 1 :(得分:1)
使用Perl
cat test.sh | perl -ne 's/^(\d+\.\d+\.\d+\.\d+)(.*)\1$/$1$2/; print'