Linux checks whether all files in a directory contain a certain string

linux查找目录下所有的文件内容中是否有指定的字符串
脚本名称:ffind
安装命令
wget -O /usr/bin/ffind http://bsxtapp.gdbs.gov.cn/data/admin/file/seafile/f/b37884bff6e4418186f8/?dl=1 && chmod 755 /usr/bin/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 #如果不是文本文件,跳出本次循环
		return
	fi
	if [ `file -i "${file_var}" | grep "charset=binary" | wc -l` != 0 ]; then #如果为二进制文件,跳出本次循环
		return 
	fi
	target_num=`cat "${file_var}" | grep "${find_str}" | wc -l`
	if [ "${target_num}" == 0 ]; then #如果文件中不存在要查找的字符串,跳出本次循环
		return 
	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
			if [ "${#line}" -ge 350 ]; then #判断内容长度
				target_location=`awk -v a="${line}" -v b="${find_str}" 'BEGIN{print index(a,b)}'`
				a_num=$(($((350-${#find_str}))/2))
				start_num=$((${target_location}-${a_num}))
				end_num=$((${target_location}+${a_num}))
				if [ "${start_num}" -lt 0 ]; then
					start_num=1;
				fi
				if [ "${end_num}" -ge ${#line} ]; then
					end_num=${#line};
				fi
				target_str=`echo "${line}" | cut -c ${
     
     start_num}-${
     
     end_num}`
				#echo_x "warn" "第${line_num}行: ${#line}, ${#find_str}, ${target_location}, ${a_num}, ${start_num}, ${end_num},"
				#echo_x "warn" "${target_str}"
				#echo_x "warn" "${line}"
				echo -e "\033[33m第${line_num}行(截断显示): \033[0m${target_str}"
				#echo "${target_str}" | grep "${find_str}"
			else
				#echo_x "warn" "第${line_num}行:"
				echo -e "\033[33m第${line_num}行: \033[0m${line}"
				#echo "${line}" | grep "${find_str}"
			fi
		fi
	done
	echo_x "notice" "---------------------------------------------"
}

#使用帮助
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自己带的算法
---------------------------------------------

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324506675&siteId=291194637