ksh:shell脚本,用于以固定间隔在目录中存在的所有文件中搜索字符串 [英] ksh: shell script to search for a string in all files present in a directory at a regular interval
问题描述
我在UNIX(SUN)中有一个目录(输出).创建了两种类型的文件,文件名带有时间戳前缀.这些文件的创建间隔为10分钟. e. g:
I have a directory (output) in unix (SUN). There are two types of files created with timestamp prefix to the file name. These file are created on a regular interval of 10 minutes. e. g:
1. 20140129_170343_fail.csv (some lines are there)
2. 20140129_170343_success.csv (some lines are there)
现在,我必须在输出目录中存在的所有文件中搜索特定的字符串,如果在失败和成功文件中找到了该字符串,我必须计算这些文件中存在的行数并将输出保存到cnt_succ
和cnt_fail
变量.如果找不到该字符串,则在20秒的睡眠定时器后,我将在同一目录中再次搜索.
Now I have to search for a particular string in all the files present in the output directory and if the string is found in fail and success files, I have to count the number of lines present in those files and save the output to the cnt_succ
and cnt_fail
variables. If the string is not found I will search again in the same directory after a sleep timer of 20 seconds.
这是我的代码
#!/usr/bin/ksh
for i in 1 2
do
grep -l 0140127_123933_part_hg_log_status.csv /osp/local/var/log/tool2/final_logs/* >log_t.txt; ### log_t.txt will contain all the matching file list
while read line ### reading the log_t.txt
do
echo "$line has following count"
CNT=`wc -l $line|tr -s " "|cut -d" " -f2`
CNT=`expr $CNT - 1`
echo $CNT
done <log_t.txt
if [ $CNT > 0 ]
then
exit
fi
echo "waiitng"
sleep 20
done
我面临的问题是,我无法在行中获取_success和_fail并检查它们的计数
The problem I'm facing is, I'm not able to get the _success and _fail in file in line and and check their count
推荐答案
最后,我能够找到解决方案.这是完整的代码:
Finaly I'm able to find the solution. Here is the complete code:
#!/usr/bin/ksh
file_name="0140127_123933.csv"
for i in 1 2
do
grep -l $file_name /osp/local/var/log/tool2/final_logs/* >log_t.txt;
while read line
do
if [ $(echo "$line" |awk '/success/') ] ## will check the success file
then
CNT_SUCC=`wc -l $line|tr -s " "|cut -d" " -f2`
CNT_SUCC=`expr $CNT_SUCC - 1`
fi
if [ $(echo "$line" |awk '/fail/') ] ## will check the fail file
then
CNT_FAIL=`wc -l $line|tr -s " "|cut -d" " -f2`
CNT_FAIL=`expr $CNT_FAIL - 1`
fi
done <log_t.txt
if [ $CNT_SUCC > 0 ] && [ $CNT_FAIL > 0 ]
then
echo " Fail count = $CNT_FAIL"
echo " Success count = $CNT_SUCC"
exit
fi
echo "waitng for next search..."
sleep 10
done
感谢大家的帮助.
这篇关于ksh:shell脚本,用于以固定间隔在目录中存在的所有文件中搜索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!