silver_noodles
silver_noodles

Reputation: 371

How to get unique results with grep?

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?

Upvotes: 3

Views: 2148

Answers (3)

Karoly Horvath
Karoly Horvath

Reputation: 96266

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.)

Upvotes: 3

Etan Reisner
Etan Reisner

Reputation: 80961

This is what the -l flag to grep is for.

-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.)

Upvotes: 0

Hans Klünder
Hans Klünder

Reputation: 2292

Pipe your text into

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

Upvotes: 1

Related Questions