欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

linux使用shell搜索查找文本的幾種方法分享

  發(fā)布時間:2012-07-30 16:57:48   作者:佚名   我要評論
linux下,日常開發(fā)的時候免不了查看開發(fā)的應(yīng)用的日志,但只要是系統(tǒng)上線,就不像原來那樣在本地調(diào)試的那么簡單了
日志的大小很大,服務(wù)器一般也是Linux、HP、AIX等,每次都下載回來就麻煩的要命,在開源中國看到下面內(nèi)容,很實用。

1.使用find和xargs命令


復(fù)制代碼
代碼如下:

find dir | xargs grep str # dir是指某個目錄
find file | xargs grep str # file是指某個文件2.


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


復(fù)制代碼
代碼如下:

grep str dir/* # dir是指某個目錄,但不遞歸搜索其子目錄
grep -r str dir/* #使用-r選項,遞歸搜索其子目錄
grep str file #file是指某個文件


3.第3種方法是綜合以上兩種


復(fù)制代碼
代碼如下:

#!/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
}

使用方法

復(fù)制代碼
代碼如下:

$ find_str $path $name

相關(guān)文章

最新評論