0

I have a list of files paths that I need to compare with a string:

git_root_path=$(git rev-parse --show-toplevel)
list_of_files=.git/ForGeneratingSBConfigAlert.txt
cd $git_root_path
echo "These files needs new OSB config:"
while read -r line
do
    modfied="$line"
    echo "File for compare: $modfied"
        if grep -qf  $list_of_files" $modfied"; then
            echo "Found: $modfied"
        fi
done < <(git status -s | grep -v " M" | awk '{if ($1 == "M") print $2}')

$modified - is a string variable that stores path to file

Pattern file example:

SVCS/resources/
SVCS/bus/projects/busCallout/
SVCS/bus/projects/busconverter/
SVCS/bus/projects/Resources/  (ignore .jar)
SVCS/bus/projects/Teema/
SVCS/common/
SVCS/domain/
SVCS/techutil/src/
SVCS/tech/mds/src/java/fi/vr/h/service/tech/mds/exception/
SVCS/tech/mds/src/java/fi/vr/h/service/tech/mds/interfaces/
SVCS/app/cashmgmt/src/java/fi/vr/h/service/app/cashmgmt/exception/
SVCS/app/cashmgmt/src/java/fi/vr/h/service/app/cashmgmt/interfaces/
SVCS/app/customer/src/java/fi/vr/h/service/app/customer/exception/
SVCS/app/customer/src/java/fi/vr/h/service/app/customer/interfaces/
SVCS/app/etravel/src/java/fi/vr/h/service/app/etravel/exception/
SVCS/app/etravel/src/java/fi/vr/h/service/app/etravel/interfaces/
SVCS/app/hermes/src/java/fi/vr/h/service/app/hermes/exception/
SVCS/app/hermes/src/java/fi/vr/h/service/app/hermes/interfaces/
SVCS/app/journey/src/java/fi/vr/h/service/app/journey/exception/
SVCS/app/journey/src/java/fi/vr/h/service/app/journey/interfaces/
SVCS/app/offline/src/java/fi/vr/h/service/app/offline/exception/
SVCS/app/offline/src/java/fi/vr/h/service/app/offline/interfaces/
SVCS/app/order/src/java/fi/vr/h/service/app/order/exception/
SVCS/app/order/src/java/fi/vr/h/service/app/order/interfaces/
SVCS/app/payment/src/java/fi/vr/h/service/app/payment/exception/
SVCS/app/payment/src/java/fi/vr/h/service/app/payment/interfaces/
SVCS/app/price/src/java/fi/vr/h/service/app/price/exception/
SVCS/app/price/src/java/fi/vr/h/service/app/price/interfaces/
SVCS/app/product/src/java/fi/vr/h/service/app/product/exception/
SVCS/app/product/src/java/fi/vr/h/service/app/product/interfaces/
SVCS/app/railcar/src/java/fi/vr/h/service/app/railcar/exception/
SVCS/app/railcar/src/java/fi/vr/h/service/app/railcar/interfaces/
SVCS/app/reservation/src/java/fi/vr/h/service/app/reservation/exception/
SVCS/app/reservation/src/java/fi/vr/h/service/app/reservation/interfaces/
kraken_test.txt
namaker_test.txt
shmaker_test.txt

I need to compare file search pattern with a string, is it possible using grep?

  • Can you state in more detail what you expect? Do you want to look for a string in a list of files? Or compare a list of files with another list? – Hellmar Becker Nov 06 '15 at 12:08
  • Am I misunderstanding something, or is it simple string comparison you are after? In that case look at http://stackoverflow.com/questions/2237080/how-to-compare-strings-in-bash-script – beruic Nov 06 '15 at 12:10
  • Do you want to grep the filename or do you need to look inside the file and do a grep on its contents? – Dominique Nov 06 '15 at 12:12

2 Answers2

1

I'm not sure I understand the overall logic, but a few immediate suggestions come to mind.

  • You can avoid grep | awk in the vast majority of cases.
  • A while loop with a grep on a line at a time inside the loop is an antipattern. You probably just want to run one grep on the whole input.

Your question would still benefit from an explanation of what you are actually trying to accomplish.

cd "$(git rev-parse --show-toplevel)"
git status -s | awk '!/ M/ && $1 == "M" { print $2 }' |
grep -Fxf .git/ForGeneratingSBConfigAlert.txt

I was trying to think of a way to add back your human-readable babble, but on second thought, this program is probably better without it.

The -x option to grep might be wrong, depending on what you are really hoping to accomplish.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

This should work:

git status -s | grep -v " M" | awk '{if ($1 == "M") print $2}' | \
grep --file=.git/ForGeneratingSBConfigAlert.txt --fixed-strings --line-regexp
  • Piping the awk output directly to grep avoids the while loop entirely. In most cases you'll find you don't really need to print debug messages and the like in it.
  • --file takes a file with one pattern to match per line.
  • --fixed-strings avoids treating any characters in the patterns as special.
  • --line-regexp anchors the patterns so that they only match if a full line of input matches one of the patterns.

All that said, could you clarify what you are actually trying to accomplish?

l0b0
  • 55,365
  • 30
  • 138
  • 223
  • The backslash isn't really necessary; a pipe at the end of line is enough to continue the pipeline on the next line. – tripleee Nov 06 '15 at 12:42
  • @tripleee I generally treat that as an anti-pattern similar to not needing `?>` at the end of PHP files and the last `;` in JavaScript. – l0b0 Nov 06 '15 at 12:45
  • I just inserted your code and it works as I need it :D I only removed the `--line-regexp` as it is not needed in my situation. I was thinking in the box(inside while loop). Thank you! – Dmitrijs Mihailovs Nov 06 '15 at 13:18