Chinaunix

标题: 新手问题[读取只以数字命名的文件到变量] [打印本页]

作者: mosoft    时间: 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变量里阿?
作者: 寂寞烈火    时间: 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
复制代码

作者: xuxingyu    时间: 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]\>' 等待高手来解答
作者: xuxingyu    时间: 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,\}$" 这样就对了
作者: mosoft    时间: 2007-01-29 23:02
都是高手阿!总算找到家了
作者: mosoft    时间: 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
执行了没效果阿。
作者: otask    时间: 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,\}$"`这个地方的$加上不能用
作者: xuxingyu    时间: 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当然没有内容
作者: mosoft    时间: 2007-01-30 10:20
哈哈,我太差劲了啊,再次改下吧
作者: mosoft    时间: 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
显示的不符合我的要求阿。
高手,帮帮我吧
作者: xuxingyu    时间: 2007-01-30 11:35
原帖由 mosoft 于 2007-1-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
显示的不符合我的要求阿 ...

我是菜鸟:)
#!/bin/sh
for file in `ls |grep "^[0-9]\{1,\}"`
do
  echo "FileName is: $file"
  more $file
done
exit 0




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2