Perl + 单行智能 Perl 命令,就地 grep 匹配异常字符 + 全匹配 [英] Perl + one-liner smart Perl command, in place grep to match unusual characters + full match

查看:60
本文介绍了Perl + 单行智能 Perl 命令,就地 grep 匹配异常字符 + 全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题:one-liner 智能 Perl 命令,而不是简单的 grep(在 Bash 脚本中)

Subject: one-liner smart Perl command instead of a simple grep (in Bash script)

我在使用 grep 匹配异常字符时遇到问题(我不能在每个异常字符前加上\",因为这很成问题,而且不是一个聪明的解决方案).

I have a problem when I use grep to match the unusual characters (I can't put the "\" before each unusual character, because it's very problematic and not a smart solution).

我需要与 $Some_string 中定义的所有不可用字符完全匹配的 Perl 单行语法.示例: 

I need Perl one-liner syntax that match exactly all the unusable characters as defined in the $Some_string. Examples: 

Examples

Some_string="[234-094]"
Some_string="[ * ( & % @ !]"
Some_string="~ [ 0:3 # % & ^ + =]"
Some_string="1.1.1.1.-9.9.9.9   + 9999.999.1 – 10000"
Some_string="< { [ ' : ; " ? / . , "
Some_string="PORT.A.B.C.D – 124.543.455.33 – [ ! NOT EQUAL PORT 38737 – 3837652"
.
.
.

我糟糕的 grep 语法

My bad grep syntax

 cat  file | grep $Some_string

使用 Perl(我需要以下正确的建议/解决方案):

With Perl (I need the following right suggestion/solution):

 cat file | perl -nle 'print if /$Some_string/'

规则

备注:if Some_string="111.111.111.111"在文件中我有 1111.1111.1111.1111那么 Perl 语法需要忽略这一点.仅当我在文件中包含以下内容时,Perl 才必须匹配: 111.111.111.111 .

Rules

Remark: if Some_string="111.111.111.111" and in the file I have 1111.1111.1111.1111 then the Perl syntax needs to ignore this. Perl must match only if I have this in the file: 111.111.111.111 .

第二点:if Some_string="WORD"在我的文件中:ONEWORD那么 Perl 语法需要忽略这一点.仅当我在文件中有这个时 Perl 才必须匹配:WORD

Second remark: if Some_string="WORD" and in the file I have: ONEWORD then the Perl syntax needs to ignore this. Perl must match only if I have this in the file: WORD

推荐答案

我不确定我是否完全理解您的问题.也许 -P-x 可以做你想做的,同时引用 "$Some_string" 来保留空格?

I'm not sure I understand your question, exactly. Perhaps -P and -x do what you want, along with quoting "$Some_string" to preserve whitespace?

grep -Px "$Some_string" file

来自 grep 手册页:

From the grep man page:

将 PATTERN 解释为 Perl 正则表达式.

-P, --perl-regexp

Interpret PATTERN as a Perl regular expression.

仅选择与整行完全匹配的匹配项.

Select only those matches that exactly match the whole line.

这篇关于Perl + 单行智能 Perl 命令,就地 grep 匹配异常字符 + 全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆