【发布时间】:2016-04-19 12:30:28
【问题描述】:
我有一个从文件名中提取日期并计算唯一天数的命令:
find xml/ -type f -name "*.201604*.xml" | head -5 | sed "s/.*\.\(2016[0-9]\{4\}\)\(.*\)/\1/g" | sort | uniq -c
在 Solaris 10 上的结果是:
# find xml/ -type f -name "*.201604*.xml" | head -5 | sed "s/.*\.\(2016[0-9]\{4\}\)\(.*\)/\1/g" | sort | uniq -c
2 20160412
1 20160417
2 20160418
# uname -a
SunOS localhost 5.10 Generic_150400-26 sun4u sparc SUNW,SPARC-Enterprise
# which sed sort
/usr/bin/sed
/usr/bin/sort
在 Solaris 11 上的结果是:
$ find xml/ -type f -name "*.201604*.xml" | head -5 | sed "s/.*\.\(2016[0-9]\{4\}\)\(.*\)/\1/g" | sort | uniq -c
1 20160401
1 20160403
1 20160405
1 xml/results/subres/ABC.DEF.GH01.20160401224003.123456.123456.xml
1 xml/results/subres/ABC.DEF.GH02.20160412124035.234567.234567.xml
$ uname -a
SunOS localhost 5.11 11.2 sun4v sparc sun4v
$ which sed sort
/usr/bin/sed
/usr/bin/sort
出于某种原因,在 Solaris 11 上,sort 命令会导致 sed 返回完整的文件路径,而不是匹配的正则表达式。
它可以在没有排序的情况下工作(Solaris 11):
$ find xml/ -type f -name "*.201604*.xml" | head -5 | sed "s/.*\.\(2016[0-9]\{4\}\)\(.*\)/\1/g"
20160403
20160401
20160401
20160412
20160405
为什么?其他人有这种行为吗?
【问题讨论】:
-
如果将
sort | uniq -c替换为简单的cat,在Solaris 11 上会得到什么输出? -
@AndrewHenle 与
sort的结果相同,它显示两个文件名而不是正则表达式结果。 -
此外,您在 Solaris 10 服务器上似乎有一个 root 提示符,这意味着您很可能正在运行 Solaris
sh- 与 Linux 不同的是,Solaris 没有混淆sh和 @987654332 @. -
@AndrewHenle,不,
sed没有别名。另外,root 提示符是假的,它是由 PS1="#" 设置的。echo $SHELL给我/usr/bin/bash。