英文:
I need a Bash that checks if a specific number, let's say 25454111, matches any of the patterns stored in a text file.can I achieve this?
问题
你可以使用以下的 Bash 脚本来实现你的需求:
#!/bin/bash
# 获取传入的数字
number=$1
# 逐行读取包含正则表达式的文件
while IFS= read -r pattern
do
# 使用 grep 进行匹配
if echo "$number" | grep -E "$pattern" > /dev/null
then
echo "匹配成功: $pattern"
exit 0
fi
done < "your_file.txt"
echo "没有找到匹配的正则表达式"
你需要将脚本保存到一个文件中(比如 check_regex.sh
),并将文件的路径替换成你的 flat file 的路径。之后,你可以通过在命令行中运行 bash check_regex.sh <number>
来检查给定的数字是否匹配任何正则表达式。
希望对你有所帮助!
英文:
Question:
I have a flat file that contains a list of numbers and regular expressions (regex) patterns. I need to write a Bash script that checks if a given number matches any of the regex patterns in the file. How can I accomplish this?
Details:
The flat file is in text format, with each line containing a single regex pattern.
The script should take a number as an argument and compare it against the regex patterns in the file.
If the number matches any of the regex patterns, the script should indicate that a match is found.
If the number does not match any of the regex patterns, the script should indicate that no match is found.
I'm looking for a Bash script that efficiently reads the file, performs the regex matching, and provides the appropriate output.
Thank you!
tried
grep -E 523167082 /etc/freeswitch/dialplan/public/oracle.txt
returns 523167082
but
grep -E 25454111 /etc/freeswitch/dialplan/public/oracle.txt
returns empty even though in the file there is a line 254541[1-4]\d
答案1
得分: 2
你这样做是反过来的。grep
的第一个参数应该是模式,而不是你要与模式匹配的字符串。你想要匹配的字符串应该在标准输入中。
如果模式存储在一个文件中,你可以使用 -f pathname
选项来从文件中获取它们。
grep -P -f oracle.txt <<< 25454111
你还需要使用 GNU grep
并使用 -P
选项来支持正则表达式中的 \d
转义序列。
英文:
You're doing this backwards. The first argument to grep
is the pattern, not the string you're trying to match against the pattern. The string you want to match should be in standard input.
If the patterns are in a file, you can use the -f pathname
option to get them from there.
grep -P -f oracle.txt <<<25454111
You also need to use GNU grep
with the -P
option to support \d
escape sequences in regular expressions.
答案2
得分: 1
以下是翻译好的部分:
样本模式文件:
$ cat oracle.txt
254541[1-4]\d
修改 OP 的当前命令以使用 oracle.txt
作为 grep
模式的来源:
$ echo 25454111 | grep -E -f oracle.txt
grep: 警告: 在 d 前出现了杂项 \
$ grep -E -f oracle.txt <<< "25454111"
grep: 警告: 在 d 前出现了杂项 \
注意:
- 旧版本的
GNU grep
(例如 3.1、3.7)会生成警告消息并返回空结果。 - 较新版本的
GNU grep
(例如 3.8)在返回空结果的同时屏蔽了警告。
\d
在 grep/ERE
中不是有效的转义序列;让我们尝试 grep/PCRE
:
$ echo 25454111 | grep -P -f oracle.txt
25454111
$ grep -P -f oracle.txt <<< "25454111"
25454111
注意: 对于测试的 GNU grep
版本(3.1、3.7、3.8)生成相同的结果。
英文:
Sample pattern file:
$ cat oracle.txt
254541[1-4]\d
Modifying OP's current command to use oracle.txt
as the source of the grep
patterns:
$ echo 25454111 | grep -E -f oracle.txt
grep: warning: stray \ before d
$ grep -E -f oracle.txt <<< "25454111"
grep: warning: stray \ before d
NOTES:
- older
GNU grep
versions (eg, 3.1, 3.7) generate the warning message and returns nothing - newer
GNU grep
versions (eg, 3.8) masks the warning while returning nothing
\d
is not a valid escape sequence in grep/ERE
; let's try grep/PCRE
:
$ echo 25454111 | grep -P -f oracle.txt
25454111
$ grep -P -f oracle.txt <<< "25454111"
25454111
NOTE: same result is generated for tested GNU grep
versions (3.1, 3.7, 3.8)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论