Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何使用grep匹配在两个字符之间找到变量的行?_Regex_Bash_Grep - Fatal编程技术网

Regex 如何使用grep匹配在两个字符之间找到变量的行?

Regex 如何使用grep匹配在两个字符之间找到变量的行?,regex,bash,grep,Regex,Bash,Grep,我需要输出文件中的所有行,其中BASH变量$a位于字母“a”和“Z”之间的某个位置,也在同一行上 A the cat went to the river Z The A cat then Z swam to the lake. Then the cat A ate Z some fish. 如果$a设置为“cat”,则只输出这些行,因为在这些行的两个字符之间可以找到“cat”: A the cat went to the river Z The A cat then Z swam to the

我需要输出文件中的所有行,其中BASH变量
$a
位于字母“a”和“Z”之间的某个位置,也在同一行上

A the cat went to the river Z
The A cat then Z swam to the lake.
Then the cat A ate Z some fish.
如果
$a
设置为“cat”,则只输出这些行,因为在这些行的两个字符之间可以找到“cat”:

A the cat went to the river Z
The A cat then Z swam to the lake.
如果
$a
设置为“then”,则仅输出此行,因为“then”位于两个字符之间:

The A cat then Z swam to the lake.
我尝试过这些方法,但都不奏效:

grep "A*[$a]+*Z" file.txt

grep "A(.*)[$a]+(.)Z" file.txt

grep "[A]+*[$a]+*[Z]+" file.txt
如何使用
grep
或类似的BASH工具匹配两个字符之间的变量行?

这应该可以:

grep "A.*$a.*Z" file.txt
这假设
$a
不包含任何在正则表达式中具有特殊含义的字符

您所有的尝试都使用
[$a]
,这是完全错误的
[chars]
匹配括号内任何一个
字符的单个字符。

这应该可以:

grep "A.*$a.*Z" file.txt
这假设
$a
不包含任何在正则表达式中具有特殊含义的字符

您所有的尝试都使用
[$a]
,这是完全错误的
[chars]
匹配括号内任何一个
字符的单个字符。

这应该可以:

grep "A.*$a.*Z" file.txt
这假设
$a
不包含任何在正则表达式中具有特殊含义的字符

您所有的尝试都使用
[$a]
,这是完全错误的
[chars]
匹配括号内任何一个
字符的单个字符。

这应该可以:

grep "A.*$a.*Z" file.txt
这假设
$a
不包含任何在正则表达式中具有特殊含义的字符


您所有的尝试都使用
[$a]
,这是完全错误的
[chars]
匹配括号内任何一个
字符的单个字符。

您应该能够只使用grep或egrep

grep "$a" filename.txt
或者,如果我理解正确,那么您希望确保每一面至少有一个字符,那么这会更好

egrep ".+$a.+" filename.txt


您应该能够只使用grep或egrep

grep "$a" filename.txt
或者,如果我理解正确,那么您希望确保每一面至少有一个字符,那么这会更好

egrep ".+$a.+" filename.txt


您应该能够只使用grep或egrep

grep "$a" filename.txt
或者,如果我理解正确,那么您希望确保每一面至少有一个字符,那么这会更好

egrep ".+$a.+" filename.txt


您应该能够只使用grep或egrep

grep "$a" filename.txt
或者,如果我理解正确,那么您希望确保每一面至少有一个字符,那么这会更好

egrep ".+$a.+" filename.txt


请尝试以下命令:

egrep  "\b(A)\b.*[\$a].*\b(Z)\b" * --color 
这在以下文本中不起作用:

A the cat went $a to the river Z and A goes to $a for Z reason.

此正则表达式为您提供
A猫将$A转到Z河,A转到$A到Z河作为输出。

尝试以下命令:

egrep  "\b(A)\b.*[\$a].*\b(Z)\b" * --color 
这在以下文本中不起作用:

A the cat went $a to the river Z and A goes to $a for Z reason.

此正则表达式为您提供
A猫将$A转到Z河,A转到$A到Z河作为输出。

尝试以下命令:

egrep  "\b(A)\b.*[\$a].*\b(Z)\b" * --color 
这在以下文本中不起作用:

A the cat went $a to the river Z and A goes to $a for Z reason.

此正则表达式为您提供
A猫将$A转到Z河,A转到$A到Z河作为输出。

尝试以下命令:

egrep  "\b(A)\b.*[\$a].*\b(Z)\b" * --color 
这在以下文本中不起作用:

A the cat went $a to the river Z and A goes to $a for Z reason.

这个正则表达式为您提供
A猫将$A转到Z河,而A将$A转到Z河作为输出。

A
awk
解决方案

awk '$0~"A.*"x".*"Z' x=$a file
A the cat went to the river Z
The A cat then Z swam to the lake.

awk
解决方案

awk '$0~"A.*"x".*"Z' x=$a file
A the cat went to the river Z
The A cat then Z swam to the lake.

awk
解决方案

awk '$0~"A.*"x".*"Z' x=$a file
A the cat went to the river Z
The A cat then Z swam to the lake.

awk
解决方案

awk '$0~"A.*"x".*"Z' x=$a file
A the cat went to the river Z
The A cat then Z swam to the lake.

你为什么把它放在括号里?你认为括号在regexp中是什么意思?为什么这么多人把括号弄错了?你为什么把它放在括号里?你认为括号在regexp中是什么意思?为什么这么多人把括号弄错了?你为什么把它放在括号里?你认为括号在regexp中是什么意思?为什么这么多人把括号弄错了?你为什么把它放在括号里?你认为括号在regexp中是什么意思?为什么有那么多人把括号弄错了?