shell查找目錄下所有的文件內容中是否有指定的字符串,並輸出數量與匹配到的行內容

腳本名稱:ffind
#!/bin/sh

#按日誌等級進行打印
echo_x()
{
	if [ "${1}" = "info" ]; then
		echo -e "\033[37m${2}\033[0m"; #白色字
	elif [ "${1}" = "notice" ];then
		echo -e "\033[36m${2}\033[0m"; #天藍字
	elif [ "${1}" = "warn" ];then
		echo -e "\033[33m${2}\033[0m"; #黃色字
	elif [ "${1}" = "error" ]; then
		echo -e "\033[31m${2}\033[0m"; #紅色字
	fi
}

#核心
core()
{
	file_var="${1}"
	find_str="${2}"
	#echo_x "info" "${file_var}"
	#echo_x "info" "${find_str}"
	if [ ! -f "${file_var}" ]; then #如果不是文本文件,跳出本次循環
		continue 
	fi
	if [ `file -i "${file_var}" | grep "charset=binary" | wc -l` != 0 ]; then #如果爲二進制文件,跳出本次循環
		continue 
	fi
	target_num=`cat "${file_var}" | grep "${find_str}" | wc -l`
	if [ "${target_num}" == 0 ]; then #如果文件中不存在要查找的字符串,跳出本次循環
		continue 
	fi
	echo_x "error" "查找到${target_num}處: ${file_var}"
	line_num=0
	cat "${file_var}" | while read line; do #遞歸文件內容
		line_num=$((${line_num} + 1)) #line_numl累加
		if [[ "${line}" == *"${find_str}"* ]]; then
			echo_x "warn" "第${line_num}行: ${line}"
		fi
	done
	echo_x "info" "---------------------------------------------"
}

#使用幫助
use_help()
{
	echo_x "error" "ffind ${*} 參數錯誤"
	echo_x "notice" "Model 1: find /home | ./ffind \"target str\""
	echo_x "notice" "Model 2: ./ffind \"target dir\" \"target str\""
}

if [ $# == 2 ]; then #遞歸目錄結構方式
	target_dir="${1}"
	find_str="${2}"
	#echo_x "info" "${target_dir}"
	#echo_x "info" "${find_str}"
	if [ ! -d "${target_dir}" ]; then #如果查找的目標不是目錄
		use_help
		exit 0
	fi
	find "${target_dir}" -type f | while read file_var; do #遞歸目錄結構
		#echo_x "info" "${file_var}"
		core "${file_var}" "${find_str}"
	done
elif [ $# == 1 ]; then #從管道中獲取數據
	var_stdin=`cat /dev/stdin`
	if [ "${#var_stdin}" != 0 ]; then #如果管道中有數據
		find_str="${1}"
		#echo_x "info" "${var_stdin}"
		for file_var in ${var_stdin}; do #遍歷管道中的數據
			#echo_x "info" "${file_var}"
			core "${file_var}" "${find_str}"
		done
	fi
else
	use_help ${*}
	exit 0
fi
使用示範1:
[root@localhost logs]# ./ffind /home/wnh/filesync/ "const string&"
查找到1處: /home/wnh/filesync/src/include/wnh_system_operation/wnh_system_operation_file_operation.cpp
第6行: //實現rm_dir(const string& path)函數刪除目錄中的所有文件,在rm_dir()中遍歷每一個文件,如果遇到目錄文件,則遞歸刪除該目錄文件。
---------------------------------------------
查找到16處: /home/wnh/filesync/src/include/wnh_openssl/wnh_openssl_string_hash.cpp
第4行: unsigned int wnh_openssl::ngx_hash_key(const string& data)//nginx中使用的simpleHash算法
第113行: int wnh_openssl::additiveHash(const string& key, int prime) //加法hash
第124行: int wnh_openssl::rotatingHash(const string& key, int prime) //旋轉hash
第135行: int wnh_openssl::oneByOneHash(const string& key) //一次一個hash
第151行: int wnh_openssl::bernstein(const string& key) //Bernstein's hash
第163行: unsigned int wnh_openssl::RSHash(const string& str) //RS算法hash
第179行: int wnh_openssl::JSHash(const string& str) //JS算法
第192行: int wnh_openssl::PJWHash(const string& str) //PJW算法
第215行: int wnh_openssl::ELFHash(const string& str) //ELF算法
第234行: uint32_t wnh_openssl::BKDRHash32(const string& str)
第247行: uint64_t wnh_openssl::BKDRHash64(const string& str)
第260行: int wnh_openssl::SDBMHash(const string& str) //SDBM算法
第273行: unsigned int wnh_openssl::DJBHash(const string& str) //DJB算法
第286行: int wnh_openssl::DEKHash(const string& str) //DEK算法
第299行: int wnh_openssl::APHash(const string& str) //AP算法
第313行: int wnh_openssl::java(const string& str) //JAVA自己帶的算法
---------------------------------------------
使用示範2(管道):
[root@localhost logs]# find /home/wnh/filesync/ | grep -E '.h$|.cpp$' | ./ffind "const string&"
查找到1處: /home/wnh/filesync/src/include/wnh_system_operation/wnh_system_operation_file_operation.cpp
第6行: //實現rm_dir(const string& path)函數刪除目錄中的所有文件,在rm_dir()中遍歷每一個文件,如果遇到目錄文件,則遞歸刪除該目錄文件。
---------------------------------------------
查找到16處: /home/wnh/filesync/src/include/wnh_openssl/wnh_openssl_string_hash.cpp
第4行: unsigned int wnh_openssl::ngx_hash_key(const string& data)//nginx中使用的simpleHash算法
第113行: int wnh_openssl::additiveHash(const string& key, int prime) //加法hash
第124行: int wnh_openssl::rotatingHash(const string& key, int prime) //旋轉hash
第135行: int wnh_openssl::oneByOneHash(const string& key) //一次一個hash
第151行: int wnh_openssl::bernstein(const string& key) //Bernstein's hash
第163行: unsigned int wnh_openssl::RSHash(const string& str) //RS算法hash
第179行: int wnh_openssl::JSHash(const string& str) //JS算法
第192行: int wnh_openssl::PJWHash(const string& str) //PJW算法
第215行: int wnh_openssl::ELFHash(const string& str) //ELF算法
第234行: uint32_t wnh_openssl::BKDRHash32(const string& str)
第247行: uint64_t wnh_openssl::BKDRHash64(const string& str)
第260行: int wnh_openssl::SDBMHash(const string& str) //SDBM算法
第273行: unsigned int wnh_openssl::DJBHash(const string& str) //DJB算法
第286行: int wnh_openssl::DEKHash(const string& str) //DEK算法
第299行: int wnh_openssl::APHash(const string& str) //AP算法
第313行: int wnh_openssl::java(const string& str) //JAVA自己帶的算法
---------------------------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章