简体   繁体   English

如何通过以下命令将bash变量传递到GREP regex中?

[英]How can I pipe a bash variable into a GREP regex in the following command?

I have a command that runs fine as follows: 我有一个可以正常运行的命令,如下所示:

grep ",162$" xyz.txt | grep -v "^162,162$"

It is basically looking for lines that have a pattern like "number,162" except those where the number is 162. 它基本上是在寻找具有“ number,162”之类的模式的行,但数字为162的行除外。

Now, I want to run many such commands where 162 is replaced with other numbers like 现在,我想运行许多这样的命令,其中162被其他数字代替

grep ",$159" xyz.txt | grep -v "^159,159$"
grep ",$111" xyz.txt | grep -v "^111,111$"

.. and so on where the same number appears in every part of the command everywhere. ..等等,相同的数字出现在命令的每个部分中。 So, I need a single place to change the value and the rest of the command can take that value wherever needed. 因此,我需要一个地方来更改值,命令的其余部分可以在需要的地方使用该值。

I tried doing something like this to repeat the number of changes I need to do when I want to try another number 我想做这样的事情来重复我想尝试另一个号码时需要做的更改次数

x=162 | grep ",\$x$" xyz.txt | grep -v "^\$x,\$x$"

but it doesn't work. 但这不起作用。 What changes should I do so that everytime I have to change the number value, I just go to the first position of the command and change only the value of x? 我应该做些什么更改,以便每次必须更改数字值时,我都只转到命令的第一个位置并仅更改x的值?

Whenever you are piping grep to grep, consider using awk: 每当将grep传递到grep时,请考虑使用awk:

var=YOUR_NUM
awk -F, -v pat="$var" '$NF==pat && $1!=pat' file

This plays a little with awk 's fields: by setting the field separator to the comma, you are then able to treat each part separately. 这与awk的字段有点用:通过将字段分隔符设置为逗号,然后可以分别处理每个部分。
You want to match those cases in which the last field is the given value and the first one is not. 您要匹配最后一个字段是给定值而第一个字段不是给定值的情况。 This is possible using $NF for the last field and using $1 for the first one. 可以在最后一个字段中使用$NF ,在第一个字段中使用$1
Also, you can add an extra layer of control by checking NF (number of fields) and making the condition be something like !($1==pat && NF==2) && $NF==pat so that a line like 162,162,162 would be printed. 另外,您可以通过检查NF (字段数)并将条件设置为!($1==pat && NF==2) && $NF==pat这样的方式来添加额外的控制层,以便像162,162,162这样的行被打印。 But this really depends on what you want to do. 但这确实取决于您要执行的操作。

For example: 例如:

$ cat file
hello
hello,162
162,162
$ val=162
$ awk -F, -v pat="$val" '$NF==pat && $1!=pat' file
hello,162

Then it is just a matter of changing $var with the value you want. 然后,只需将$var更改$var所需的值即可。

If I understand it correctcly, you want something like that: 如果我对它的理解正确,则您需要以下内容:

x=162
grep ",$x\$" xyz.txt | grep -v "^$x,$x\$"

如果您打算单线报价,那就是您的朋友:

X=123 /bin/sh -c 'grep ","$X"$" xyz.txt | grep -v "^"$X","$X"$"'

Single grep command can be used with negative lookbehind: 单个grep命令可以与否定性后面使用:

$ cat xyz.txt 
12,12
112,12
1,12
42,12

$ grep -P '(?<!^12),12$' xyz.txt
112,12
1,12
42,12

Passing variable is tricky 传递变量很棘手

$ x=12
$ grep -P "(?<!"^$x"),"$x"$" xyz.txt
112,12
1,12
42,12

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

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