如何在连续逐行读取文件的同时从apache服务器的error.log文件中分割文件?

发布于 2024-10-01 03:55:10 字数 2518 浏览 3 评论 0原文

我已经这样做了,但我编写的 shellscript 遇到了问题。我对 tail 命令功能感到困惑,而且当我在终端上看到 error.log 的输出时,它显示从单词中删除了“e”的行。

我是这样写的,请指导我如何解决我的问题。我想逐行读取这个 error.log 文件,在读取行期间,我想将固定数量的行拆分为后缀为 log-aa、log-ab、... 的小文件,我使用 split 命令执行此操作。分割后,我想使用正则表达式过滤其中包含 GET 或 POST 单词的行,并将这些过滤后的行存储到新文件中。该存储完成后,我需要删除所有这些 log-* 文件。

我是这样写的:

enter code here
processLine(){
  line="$@"
  echo $line
  $ tail -f $FILE
}
FILE="/var/log/apache2/error.log"
if [ "$1" == "/var/log/apache2/error.log" ]; then
   FILE="/dev/stdin"
else
   FILE="$1"
   if [ ! -f $FILE ]; then
   echo "$FILE : does not exists"
   exit 1
   elif [ ! -r $FILE ]; then
   echo "$FILE: can not read"
   exit 2
   fi
fi
#BAKIFS=$IFS
#IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
#sed -e 's/\[debug\].*\(data\-HEAP\)\:\/-->/g' error.log > /var/log/apache2/error.log.1
while read -r line
do
 processLine $line
done
exec 0<&3
IFS=$BAKIFS
logfile="/var/log/apache2/error.log"
pattern="bytes"
# read each new line as it gets written
# to the log file
#tail -1 $logfile 
tail -fn0 $logfile | while read line ; do
# check each line against our pattern
echo "$line" | grep -i "$pattern" 
#sed -e 's/\[debug\].*\(data\-HEAP\)\:/-->/g' error.log >/var/log/apache2/error.log
split -l 1000 error.log log-  
FILE2="/var/log/apache2/log-*"
if [ "$1" == "/var/log/apache2/log-*" ]; then
   FILE2="/dev/stdin"
else
   FILE2="$1"
   if [ ! -f $FILE2 ]; then
   echo "$FILE : does not exists"
   exit 1
   elif [ ! -r $FILE2 ]; then
   echo "$FILE: can not read"
   exit 2
   fi
fi
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE2"
while read -r line
do
 processLine $line
 echo $line >>/var/log/apache2/url.txt
done
exec 0<&3
IFS=$BAKIFS
find . -name "var/log/apache2/logs/log-*.*" -delete
done
exit 0

下面的代码在读取和分割 error.log 后删除文件,但是当我放置 tail -f $FILE 时,它会停止删除文件我想在 log-* 文件到达 error.log 文件的最后一行后删除文件: 在此输入代码

processLine(){
line="$@" 
echo $line
}
FILE=""
if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2                                     
fi
fi
#BAKIFS=$IFS
#IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
processLine $line
split -l 1000 error.log log-
cat log-?? | grep "GET\|POST" > storefile 
#tail -f $FILE
done 
rm log-??
exec 0<&3 
#IFS=$BAKIFS
exit 0

I have done like this but I am having trouble with shellscript I have written. I am confused with tail command functionality and also when I see output of error.log on terminal it shows lines with 'e' deleted from words.

I have written like this please guide me how can I get my problem solved. I want to read this error.log file line by line and during reading lines I want to split fixed number of lines to small files with suffix i.e log-aa,log-ab,... I did this using split command. After splitting I want to filter lines with GET or POST word in them using regex and store this filtered lines into new file. After this store gets completed I need to delete all these log-* files.

I have written like this:

enter code here
processLine(){
  line="$@"
  echo $line
  $ tail -f $FILE
}
FILE="/var/log/apache2/error.log"
if [ "$1" == "/var/log/apache2/error.log" ]; then
   FILE="/dev/stdin"
else
   FILE="$1"
   if [ ! -f $FILE ]; then
   echo "$FILE : does not exists"
   exit 1
   elif [ ! -r $FILE ]; then
   echo "$FILE: can not read"
   exit 2
   fi
fi
#BAKIFS=$IFS
#IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
#sed -e 's/\[debug\].*\(data\-HEAP\)\:\/-->/g' error.log > /var/log/apache2/error.log.1
while read -r line
do
 processLine $line
done
exec 0<&3
IFS=$BAKIFS
logfile="/var/log/apache2/error.log"
pattern="bytes"
# read each new line as it gets written
# to the log file
#tail -1 $logfile 
tail -fn0 $logfile | while read line ; do
# check each line against our pattern
echo "$line" | grep -i "$pattern" 
#sed -e 's/\[debug\].*\(data\-HEAP\)\:/-->/g' error.log >/var/log/apache2/error.log
split -l 1000 error.log log-  
FILE2="/var/log/apache2/log-*"
if [ "$1" == "/var/log/apache2/log-*" ]; then
   FILE2="/dev/stdin"
else
   FILE2="$1"
   if [ ! -f $FILE2 ]; then
   echo "$FILE : does not exists"
   exit 1
   elif [ ! -r $FILE2 ]; then
   echo "$FILE: can not read"
   exit 2
   fi
fi
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE2"
while read -r line
do
 processLine $line
 echo $line >>/var/log/apache2/url.txt
done
exec 0<&3
IFS=$BAKIFS
find . -name "var/log/apache2/logs/log-*.*" -delete
done
exit 0

The below code deletes files after reading and splitting error.log but when I put tail -f $FILE it stops deleting files I want to delete log-* files after it reaches last line of error.log file:
enter code here

processLine(){
line="$@" 
echo $line
}
FILE=""
if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE="$1"
# make sure file exist and readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: can not read"
exit 2                                     
fi
fi
#BAKIFS=$IFS
#IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<"$FILE"
while read -r line
do
processLine $line
split -l 1000 error.log log-
cat log-?? | grep "GET\|POST" > storefile 
#tail -f $FILE
done 
rm log-??
exec 0<&3 
#IFS=$BAKIFS
exit 0

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风起 2024-10-08 03:55:10

您的代码看起来不必要地冗长和复杂,而且逻辑也不清楚。它不应该在无法正常工作的情况下增长得这么大。

考虑一下:

split -l 1000 error.log log-
cat log-?? | grep "GET\|POST" > storefile
rm log-??

尝试一下。如果这三个命令符合您的预期,您可以添加更多功能(例如使用路径、检查 error.log 是否存在),但在这部分工作之前不要添加代码。

Your code seems unnecessarily long and complex, and the logic is unclear. It should not have been allowed to grow this big without working correctly.

Consider this:

split -l 1000 error.log log-
cat log-?? | grep "GET\|POST" > storefile
rm log-??

Experiment with this. If these three commands do what you expect, you can add more functionality (e.g. using paths, checking for the existence of error.log), but don't add code until you have this part working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文