How can I get unique results using grep?

The script below is part of the logic 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 find a specific line . An example of the result that grep returns looks 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 like:

/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 maven command. So:

  • How can I extract the unique file paths from the result set given by the grep command?

  • How do I start a loop like a thing where on each iteration I execute the mvn command with a unique file path?

+3


source to share


3 answers


If you only want the name of the corresponding files, grep

has a command line switch for that:



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

      

+2


source


Draw text in



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

      

+1


source


This flag is -l

for grep

for.

-l, --files-with-matches

Suppress normal output; instead, print the name of each input file from which the output would normally be printed. Scanning will stop at the first match. (-l is specified by POSIX.)

0


source







All Articles