How to sort find result such that paths beginning with one of a set of patterns are sorted last

StackOverflow https://stackoverflow.com/questions/3697457

Pergunta

I have a find command that I would like to sort such that entries for certain directories are sorted last. The reason is that this list is to be passed to etags to create a tags table and I would like certain third-party tool directories to be after all the code I actively edit.

Can someone suggest a good easy way in to sort the list as a change to my makefile rule? Here is the current rule:

tags:
 rm -f ../TAGS
 find .. \( -not -regex '.*include/.*' \)   \
  -a \( -name '*.h' -o -name '*.hh' -o -name '*.y' \
   -o -name '*.l' -o -name '*.cc' -o -name '*.cpp' \
   -o -name '*.c' -o -name '*.inl' \)  \
  | xargs etags -o ../TAGS --append

For example, entries that begin "../flexlm/" or "../src/librsync" should come after entries that don't match one of these patterns.

Foi útil?

Solução

Put multiple find commands in a brace block and pipe that into xargs:

# the single quotes take care of the escaping
pattern='( -not -regex ".*include/.*" )
         -a ( -name "*.h" -o -name "*.hh" -o -name "*.y"
         -o -name "*.l" -o -name "*.cc" -o -name "*.cpp"
         -o -name "*.c" -o -name "*.inl" )'

{
  find ! -path "../flexlm/*" ! -path "../src/librsync/*" $pattern
  find -path "../flexlm/*" $pattern
  find -path "../src/librsync/*" $pattern
} | xargs etags -o ../TAGS --append

Outras dicas

Well assuming you can afford to run multiple find queries and you have your project set up in such a way that it is possible to find your own source files with one query and any libraries with other queries...

... That'd be what I'd do.

Here is what worked for me by combining the above answers and tweaking them:

PATTERN := \( -not -regex '.*include/.*' \)             \
        -a \( -name '*.h' -o -name '*.hh' -o -name '*.y'    \
            -o -name '*.l' -o -name '*.cc' -o -name '*.cpp' \
            -o -name '*.c' -o -name '*.inl' \)

.PHONY: tags
tags:
    rm -f ../TAGS
    find ..                             \
        ! -path "../src/librsync/*"             \
        ! -path "../flexlm/*"                   \
         $(PATTERN) | xargs etags -o ../TAGS --append
    find .. -path "../src/librsync/*"               \
         $(PATTERN) | xargs etags -o ../TAGS --append
    find .. -path "../flexlm/*"                 \
         $(PATTERN) | xargs etags -o ../TAGS --append
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top