Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.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/0/assembly/5.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
Bash 使用grep查找精确匹配_Bash - Fatal编程技术网

Bash 使用grep查找精确匹配

Bash 使用grep查找精确匹配,bash,Bash,假设我有这样一个文件: tst.txt fName1 lName1-a 222 fname1 lName1-b 22 fName1 lName1 2 我想使用以下命令仅获取“fName1 lName1”的第3列: var=`grep -i -w "fName1 lName1" tst.txt` 但是,这会返回以“fName1 lName1”开头的每一行,如何查找精确匹配?您可以尝试此方法: grep-i-E“^fName1 lName1\s”tst.txt|cut-f3,3--d” 但您必

假设我有这样一个文件:

tst.txt

fName1 lName1-a  222
fname1 lName1-b 22
fName1 lName1 2
我想使用以下命令仅获取“fName1 lName1”的第3列:

var=`grep -i -w "fName1 lName1" tst.txt`

但是,这会返回以“fName1 lName1”开头的每一行,如何查找精确匹配?

您可以尝试此方法:
grep-i-E“^fName1 lName1\s”tst.txt|cut-f3,3--d”

但您必须确保该行以
fName1
开头,并且
lName1
后面有空格

var=$(grep "fName1 lName1 " tst.txt |cut -d ' ' -f 3)
#!/bin/bash

var=$(grep -Po '(?<=fName1 lName1 ).+' tst.txt)
echo $var
#/bin/bash

var=$(grep-Po'(?空格是可以在grep中使用的分隔符吗?如果是,请在grep中使用它以防止“1Name1-a”被发现。我尝试使用空格,但无法使其工作。请尝试以下操作:
grep-Po'(?你能准确地解释一下为什么你不能使用空格吗?它应该而且确实如此:请注意,grep在这个答案中使用的模式有一个尾随空格。这个答案上面的答案使用尾随空格来匹配所需的行,而不是其他行。