免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 2339 | 回复: 10
打印 上一主题 下一主题

新手问题[读取只以数字命名的文件到变量] [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-29 16:56 |只看该作者 |倒序浏览
在《linux程序设计》里看到一个程序 first.sh

#!/bin/sh

# first.sh
# This file looks through all the files in the current
# directory for the string POSIX, and then prints those
# files to the standard output.

for file in *
do
  if grep -q POSIX $file
  then
    more $file
  fi
done

exit 0
好像是浏览所有文件,并找出里面有POSIX字符串的文件并打印到屏幕

我现在想改进到,只浏览以数字命名的文件
比如说1,2,3,4,5等等
文件名字是规律的整数,但是个数不一定。
请问高手,怎么读取到file变量里阿?

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
2 [报告]
发表于 2007-01-29 17:08 |只看该作者

  1. find /path -type f|grep '\b[0-9]\+\b'|while read file;do grep -q patten $file && more $file;done
复制代码

论坛徽章:
0
3 [报告]
发表于 2007-01-29 17:35 |只看该作者
原帖由 mosoft 于 2007-1-29 16:56 发表
在《linux程序设计》里看到一个程序 first.sh

#!/bin/sh

# first.sh
# This file looks through all the files in the current
# directory for the string POSIX, and then prints those
# files to th ...

这种实现起来没做过,不过应有人会吧,我只会以数字开头和以数字结尾的,你的文件名数字个数不确定这个不太好办grep '^[0-9].*[0-9]\>' 等待高手来解答

论坛徽章:
0
4 [报告]
发表于 2007-01-29 17:44 |只看该作者
原帖由 mosoft 于 2007-1-29 16:56 发表
在《linux程序设计》里看到一个程序 first.sh

#!/bin/sh

# first.sh
# This file looks through all the files in the current
# directory for the string POSIX, and then prints those
# files to th ...

ls | grep "^[0-9]\{1,\}$" 这样就对了

论坛徽章:
0
5 [报告]
发表于 2007-01-29 23:02 |只看该作者
都是高手阿!总算找到家了

论坛徽章:
0
6 [报告]
发表于 2007-01-29 23:25 |只看该作者
但是高手请把程序修改好了,告诉我阿?
我调试了下阿
对于4楼的,直接执行是可以的阿,可是我写了如下代码
#!/bin/sh

# first.sh
# This file looks through all the files in the current
# directory for the string POSIX, and then prints those
# files to the standard output.

for file in `ls | grep "^[0-9]\{1,\}$"`
do
  if grep -q POSIX $file
  then
    more $file
  fi
done

exit 0
执行了没效果阿。

论坛徽章:
0
7 [报告]
发表于 2007-01-30 00:02 |只看该作者
原帖由 mosoft 于 2007-1-29 23:25 发表
但是高手请把程序修改好了,告诉我阿?
我调试了下阿
对于4楼的,直接执行是可以的阿,可是我写了如下代码
#!/bin/sh

# first.sh
# This file looks through all the files in the current
# directory f ...



grep "^[0-9]\{1,\}$"`这个地方的$加上不能用

论坛徽章:
0
8 [报告]
发表于 2007-01-30 09:21 |只看该作者
原帖由 mosoft 于 2007-1-29 23:25 发表
但是高手请把程序修改好了,告诉我阿?
我调试了下阿
对于4楼的,直接执行是可以的阿,可是我写了如下代码
#!/bin/sh

# first.sh
# This file looks through all the files in the current
# directory f ...

你数字命名的文件不符合grep -q POSIX $file当然没有内容

论坛徽章:
0
9 [报告]
发表于 2007-01-30 10:20 |只看该作者
哈哈,我太差劲了啊,再次改下吧

论坛徽章:
0
10 [报告]
发表于 2007-01-30 11:15 |只看该作者
我改成这样子了
#!/bin/sh
for file in *
do
  if grep "^[0-9]\{1,\}" $file
  then
    echo "FileName is: $file"
    more $file
  fi
done

exit 0
显示的不符合我的要求阿。
高手,帮帮我吧
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP