LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 996|回复: 2

这个script如何写

[复制链接]
发表于 2007-1-23 22:58:07 | 显示全部楼层 |阅读模式
根据自己输入的参数, 从一个文件里面grep 出需要的信息。
例如,
如果你输入数字5(0--9),就把所有包含5的行全部显示出来
如果你输入"string",就把所有包含"string"的行全部显示出来
如果你输入[string]",就把所有包含[string]的行全部显示出来
如果你输入<string>,就把所有包含<string>的行全部显示出来
如果你输入数字string_hello就把所有包含string_hello的行全部显示出来

可以输入多个参数,就要把同时包含这几个参数的行显示出来

小弟花了好久时间都没有写出来,现在提出来让各位帮忙!谢谢各位大虾
发表于 2007-1-24 15:41:43 | 显示全部楼层
我编了一个shell, 简单得把所有的参数一视同仁,代码如下

  1. #! /bin/bash
  2. # shellname: grep_mul_str
  3. # function: Use 'grep' to find lines with multiple token

  4. if [[ $# = 0 || $# = 1 ]]
  5. then
  6.    echo "Usage: `basename $0` filename string1 string2 ... "
  7.    exit 1
  8. fi

  9. if [[ ! -e $1 ]]
  10. then
  11.    echo "File $1 not exist!"
  12.    exit 2
  13. fi

  14. touch tmp1
  15. touch tmp2
  16. cp -f $1 tmp1
  17. while [ "$2" ]
  18. do
  19.    grep "$2" tmp1 > tmp2
  20.    cp -f tmp2 tmp1
  21.    shift
  22. done

  23. cat tmp2
  24. rm -f tmp1
  25. rm -f tmp2
复制代码


输入文件datafile

  1. bill stone:hust:24/3/2005
  2. abc sfals  3
  3. askdlfjl aisl
  4. whiel abc 3
  5. alsdkj lasi abc
  6. aslkdfj a  a  a
复制代码


执行命令
$ ./grep_mul_str datafile abc 3
abc sfals  3
whiel abc 3
$
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-1-24 23:20:07 | 显示全部楼层

这个script如何写

如果在datafile里面有
[abcd]
[cdef]
<hhhh>

执行命令
$ ./grep_mul_str datafile [abcd]

result:

[abcd]
[cdef]
<hhhh>
结果应该是只有一行才是正确的,请继续帮忙看看。Thanks a lot
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表