问题
Here is my Makefile and the output, how to print the range like [1-5, 7, 9-10]
?
$ ls /tmp/foo
foo10.txt foo1.txt foo2.txt foo3.txt foo4.txt foo5.txt foo7.txt foo9.txt
$ cat Makefile
DIR1 := /tmp/foo/
COMMA :=,
EMPTY :=
SPACE := $(EMPTY) $(EMPTY)
VERSIONS := $(subst $(SPACE),$(COMMA),$(patsubst foo%,%,$(basename $(notdir $(wildcard $(DIR1)/foo*.txt)))))
all:
$(info versions is [${VERSIONS}])
$ make
versions is [10,1,2,3,4,5,7,9]
回答1:
Here is pipeline of shell commands to get your job done:
printf '%s\n' foo*[0-9].txt |
sed 's/[^0-9]*//g' |
sort -n |
awk 'function prnt(sep){printf "%s%s", s, (p > s ? "-" p : "") sep}
NR==1{s = $1} p < $1-1{prnt(","); s = $1} {p = $1} END{prnt(ORS)}'
1-5,7,9-10
Commands are:
printf
to print each filename on separate linessed
to remove everything except digitssort
to sort numerically to get the range in right sequenceawk
to set the range and format the results
回答2:
There are several approaches:
- If you know the maximum possible value: First, add
SHELL=bash
so that brace espansion works. Then use something like$(shell {1..10})
, and pass that result through$(wildcard)
to exclude non-existent files. - Pipe lines through
sort -n
, again in$(shell)
. - Pad numbers on the left with
0
s, then sort normally.
来源:https://stackoverflow.com/questions/46288292/how-to-compute-the-range-in-makefile