欢迎登陆真网站,您的到来是我们的荣幸。 登陆 注册 忘记密码? ☆设为首页 △加入收藏
欢迎加入真幸福QQ群
电脑知识: 基础知识 网络技术 操作系统 办公软件 电脑维修 电脑安全 windows7 windows8 windows10 服务器教程 平板电脑 视频播放教程 网络应用 互联网 工具软件 浏览器教程 QQ技巧 输入法教程 影视制作 YY教程 wps教程 word教程 Excel教程 PowerPoint
云南西双版纳特产小花糯玉米真空包装


Windows 8.1 Preview安装无法完成
Istanbul Linux下的屏幕录像机介绍
linux下管理员强行踢出用户的命令使用方法
Word2013如何制作标准格式的标题
linux usermod命令参数及用法详解
linux userdel命令参数及用法详解
Word2013怎么设置图片背景透明
Win8的新热键有哪些
linux chsh命令参数及用法详解
Excel怎么制作柱状折线图
linux使用shell搜索查找文本的几种方法分享
【 来源:网络 】【 点击:1 】 【 发布时间:2017_03_03 08:59:59 】

   日志的大小很大,服务器一般也是Linux、HP、AIX等,每次都下载回来就麻烦的要命,在开源中国看到下面内容,很实用。

  1.使用find和xargs命令

  代码如下:

  find dir | xargs grep str # dir是指某个目录

  find file | xargs grep str # file是指某个文件2.

  第2种方法是直接使用grep命令

  代码如下:

  grep str dir/* # dir是指某个目录,但不递归搜索其子目录

  grep -r str dir/* #使用-r选项,递归搜索其子目录

  grep str file #file是指某个文件

  3.第3种方法是综合以上两种

  代码如下:

  #!/bin/bash

  #find_str.sh

  if [ $# -lt "2" ]; then

  echo "Usage: `basename $0` path name [option]"

  exit 1

  fi

  #!-r表示递归处理子目录,-i表示忽略大小写

  path=$1

  name=$2

  shift

  shift

  for option in "$@"

  do

  case $option in

  -r) dir_op="-r"

  ;;

  -i) lu_op="-i"

  ;;

  *) if [ -n "$option" ]; then

  echo "invalid option"

  exit 1

  fi

  ;;

  esac

  done

  grep_str_of_file()

  {

  file=$1

  str=$2

  out=$(grep -n $lu_op "$str" "$file")

  if [ -n "$out" -a "$file" != "$0" ]; then

  echo "$file: $out"

  fi

  }

  find_str()

  {

  if [ -d "$1" ]; then

  for file in $1/*

  do

  if [ "$dir_op" = "-r" -a -d "$file" ]; then

  find_str $file $2

  elif [ -f "$file" ]; then

  grep_str_of_file $file $2

  fi

  done

  elif [ -f "$1" ]; then

  grep_str_of_file $1 $2

  fi

  }

  使用方法

  代码如下:

  $ find_str $path $name

本网站由川南居提供技术支持,fkzxf版权所有 浙ICP备12031891号
淳安分站 淳安分站