Remove string from line in shell and python

时间:2015-05-12 23:44:44

标签: python linux bash shell awk

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.

2 个答案:

答案 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

使用awk

这是从行的末尾删除第二次出现的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"$", "")用空字符串替换这种重新出现。

使用shell

$ 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

这将查找该行末尾重复该行第一个单词的任何行。如果是,则删除重复:

$ sed -r 's/([^ ]*)( .*)\1$/\1\2/' test.sh
173.36.31.10 dasdsafafa fafsafaasfa fafasfaa
173.36.31.11 dasdsafafa fafsafaasfa fafasfaa

使用python

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'