如何在Makefile中计算范围 [英] How to compute the range in Makefile

查看:256
本文介绍了如何在Makefile中计算范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Makefile和输出,如何打印[1-5, 7, 9-10]之类的范围?

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]

推荐答案

以下是Shell命令的管道,可以完成您的工作:

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

命令是:

  1. printf在单独的行上打印每个文件名
  2. sed删除除数字外的所有内容
  3. sort进行数字排序以获得正确的范围
  4. awk设置范围并设置结果格式
  1. printf to print each filename on separate lines
  2. sed to remove everything except digits
  3. sort to sort numerically to get the range in right sequence
  4. awk to set the range and format the results

这篇关于如何在Makefile中计算范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆