Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get unique results with grep?

Tags:

bash

The below mentioned scenario is a part of the logic that i want to implement as part of a jenkins job. I am trying to write a shell script.

I am using grep command to recursively search for a particular string. Sample result that grep returns is like this:

./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" })

I want to extract unique file paths such as:

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

and then use each unique path in a maven command. So:

  1. How can i extract unique file paths from the result set given by grep command?

  2. How do i run a loop kind of a thing, where in every iteration i execute mvn command with unique file path?

like image 643
silver_noodles Avatar asked Dec 13 '25 08:12

silver_noodles


2 Answers

If you need only the name of the matching files, grep has a command line switch for this:

-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.)
like image 54
Karoly Horvath Avatar answered Dec 16 '25 09:12

Karoly Horvath


Pipe your text into

sed 's/:.*//' | sort -u | while read path
do
    echo now execute your command using "$path"
done
like image 23
Hans Klünder Avatar answered Dec 16 '25 09:12

Hans Klünder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!