简体   繁体   English

我怎样才能将此 egrep 脚本更改为 zgrep 脚本并且仍然可以工作?

[英]How could I change this egrep script to a zgrep script and still have it work?

I'm trying to look for phone numbers in any of the following formats: +1.570.555.1212, 570.555.1212, (570)555-1212, and 570-555-1212.我正在尝试查找以下任一格式的电话号码:+1.570.555.1212、570.555.1212、(570)555-1212 和 570-555-1212。 We also need to look in compressed folders using zgrep, however I would have my code come back "No matches found".我们还需要使用 zgrep 查看压缩文件夹,但是我会让我的代码返回“未找到匹配项”。 The code is working as it is below to find phone numbers from txt files.代码如下所示,可以从 txt 文件中查找电话号码。 It is very bad, but here it is below这是非常糟糕的,但它在下面

Code:代码:

#!/bin/bash
egrep '[0-9]{3}-[0-9]{3}-[0-9]{4}|[0-9]{3}.[0-9]{3}.[0-9]{4}|([0-9]{3})[0-9]{3}-[0-9]{4}|+(1).[0-9]{3}.[0-9]{3}.[0-9]{4}' *
if [ $? -eq 0 ] ; then echo $1 ; else echo "No matches found" ; fi 2>/dev/null

zgrep without any options is equivalent in its regex capabilities to grep ;没有任何选项的zgrep的正则表达式功能等同于grep you need to say zgrep -E if you want to use grep -E (aka egrep ) regex syntax when searching compressed files.如果要在搜索压缩文件时使用grep -E (又名egrep )正则表达式语法,则需要说zgrep -E

#!/bin/bash
if zgrep -E -q '[0-9]{3}-[0-9]{3}-[0-9]{4}|[0-9]{3}.[0-9]{3}.[0-9]{4}|([0-9]{3})[0-9]{3}-[0-9]{4}|+(1).[0-9]{3}.[0-9]{3}.[0-9]{4}' *
then
    echo "$1"
else
    echo "No matches found" >&2
fi

Notice also Why is testing “$?”还要注意为什么要测试“$?” to see if a command succeeded or not, an anti-pattern? 查看命令是否成功,反模式? and When to wrap quotes around a shell variable as well as the preference for -q over redirecting to /dev/null , and the displaying of error messages on standard error ( >&2 redirection).以及何时在 shell 变量周围加上引号,以及-q优先于重定向到/dev/null以及在标准错误( >&2重定向)时显示错误消息。

Your regex could also use some refactoring;您的正则表达式也可以使用一些重构; maybe try也许试试

(\+\(1\).)?[0-9]{3}.[0-9]{3}.[0-9]{4}

Notice how round brackets and the plus sign need to be backslash-escaped to match literally, and how after refactoring out the +(1) prefix as optional the rest of the regex subsumes all the other variants you had enumerated, because .请注意圆括号和加号需要如何反斜杠转义以匹配字面意思,以及在将+(1)前缀重构为可选后,正则表达式的 rest 包含您枚举的所有其他变体,因为. matches - and ( and . and many other characters. (The optional prefix could also be dropped completely and this would still match the same strings, but I had to guess some things so I am leaving it in with this remark.)匹配-(.以及许多其他字符。(可选前缀也可以完全删除,这仍然会匹配相同的字符串,但我不得不猜测一些事情,所以我把它留在这个评论中。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM