shell字符串替换问题

Beanocean 2014-08-08 12:34:30
我想实现这样一个功能,shell中对如下的列表进行遍历(列表中是如下的一串数字):

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022

我想删除其前导的0,这么写的:

for x in $(seq -w 100);do
echo ${x##0*}
done

这里${x##[patter]}这种方法为什么将*当做了通配符使用?而不是将0重复若干遍?我把pattern写成00*同样错误
我该如何表示将0重复至少一遍这个模式呢?
...全文
247 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
buyong 2014-08-18
  • 打赏
  • 举报
回复
sed 's/^0*//' test: $ echo 001 | sed 's/^0*//' 1 $ echo 010 | sed 's/^0*//' 10 $ echo 111 | sed 's/^0*//' 111 $ echo 100 | sed 's/^0*//' 100
ljc007 2014-08-18
  • 打赏
  • 举报
回复
引用 7 楼 cherry532 的回复:
这个就可以了 sed 's/^0//'
你没做测试吧 [root]# echo 001 | sed 's/^0//' 01
cherry532 2014-08-17
  • 打赏
  • 举报
回复
这个就可以了 sed 's/^0//'
endeavourken 2014-08-16
  • 打赏
  • 举报
回复
先把答案给你。 如果是在脚本里写的话,先把extended patter matching打开 shopt -s extglob for i in $(cat temp) #这里假设temp是楼主放数据的文件 do i=${i##*(0)} echo $i done 就可以将前面的0全删掉了。至于为什么,建议楼主man bash以下。 然后在1062-1092行中间,这么说的 ${parameter##word} Remove matching prefix pattern. The word is expanded to produce a pattern just as in pathname expansion. 在parameter expansion(参数展开)中,知道${parameter##word}中的word是按照pathname expansion进行的,不是正则表达式(grep)类型的,所以不能在后面加*来实现匹配0到无穷次。 pathname expansion在bash的手册里也有, 1229-1259行中提到 If the extglob shell option is enabled using the shopt builtin, several extended pattern matching operators are rec‐ ognized. In the following description, a pattern-list is a list of one or more patterns separated by a |. Compos‐ ite patterns may be formed using one or more of the following sub-patterns: ?(pattern-list) Matches zero or one occurrence of the given patterns *(pattern-list) Matches zero or more occurrences of the given patterns +(pattern-list) Matches one or more occurrences of the given patterns @(pattern-list) Matches one of the given patterns !(pattern-list) Matches anything except one of the given patterns 所以这里,我用*(0)就可以匹配任意次0了
cainiao000 2014-08-08
  • 打赏
  • 举报
回复
for x in $(seq 1 100 );do echo $x done
ljc007 2014-08-08
  • 打赏
  • 举报
回复
seq -w 100 | sed 's/^0\+//'
cainiao000 2014-08-08
  • 打赏
  • 举报
回复
seq 1 100
Beanocean 2014-08-08
  • 打赏
  • 举报
回复
引用 1 楼 cainiao000 的回复:
echo ${x##0*0}
这样的话像010这样的就会被删掉....
cainiao000 2014-08-08
  • 打赏
  • 举报
回复
echo ${x##0*0}

19,613

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧