如何用grep获得独特的结果?

时间:2014-12-31 18:33:50

标签: bash

下面提到的场景是我想要作为jenkins工作的一部分实现的逻辑的一部分。我正在尝试编写一个shell脚本。

我正在使用grep命令递归搜索特定字符串。 grep返回的示例结果如下:

./src/test/java/com/ABC/st/test/pricing/Test1.java: @Tags({ "B-05256" })
./src/test/java/com/ABC/st/test/pricing/Test1.java: @MapToVO(storyID = "B-05256: prices in ST") 
./src/test/java/com/ABC/st/test/pricing/Test1.java: @Tags({ "B-05256" })
./src/test/java/com/ABC/st/test/pricing/Test2.java: @Tags({ "B-05256" })
./src/test/java/com/ABC/st/test/pricing/Test2.java: @MapToVO(storyID = "B-05256:Lowest Price of the Season")    
./src/test/java/com/ABC/st/test/pricing/Test2.java: @Tags({ "B-05256" })

我想提取唯一的文件路径,例如:

/src/test/java/com/ABC/st/test/pricing/Test1.java
/src/test/java/com/ABC/st/test/pricing/Test2.java

然后在maven命令中使用每个唯一路径。所以:

  1. 如何从grep命令提供的结果集中提取唯一文件路径?

  2. 我如何运行循环类的东西,在每次迭代中我用唯一的文件路径执行mvn命令?

3 个答案:

答案 0 :(得分:3)

如果只需要匹配文件的名称,grep有一个命令行开关:

-l, --files-with-matches
       Suppress  normal  output; instead print the name of each input file from which output
       would normally have been printed.  The scanning will stop on the first match.  (-l is
       specified by POSIX.)

答案 1 :(得分:1)

将文字传输到

sed 's/:.*//' | sort -u | while read path
do
    echo now execute your command using "$path"
done

答案 2 :(得分:0)

这是-l grep的标志。

  

-l, - with-matches-matches

     

抑制正常输出;而是打印通常打印输出的每个输入文件的名称。扫描将在第一场比赛中停止。 (-l由POSIX指定。)