bash脚本/ while循环非常慢读入文件 [英] Bash script / while loop extremely slow read file

查看:164
本文介绍了bash脚本/ while循环非常慢读入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个while循环,在FTP日志文件中读取并把它变成一个数组,所以我就可以通过数组搜索和匹配的流量/搜索。不幸的是,while循环永远走通过文件来获得,这是一个非常大的文件,但必须有这样做的另一种更快的方式。

 #读取文件到阵列的原始搜索结果
而读FTP_SEARCH

ogl_date [计数] ='回声$ FTP_SEARCH | AWK'{打印$ 1,$ 2}'`
ogl_time [统计] ='回声$ FTP_SEARCH | AWK'{打印$ 3}'`
ogl_server [统计] ='回声$ FTP_SEARCH | AWK'{打印$ 4}'`
ogl_id [统计] ='回声$ FTP_SEARCH | AWK'{打印$ 5}'`
ogl_type [统计] ='回声$ FTP_SEARCH | awk的-F'[''{打印$ 1}'| AWK'{打印$ 5}'`
ogl_pid [计数] ='回声$ FTP_SEARCH | awk的-F'[''{打印$ 2}'| awk的-F']''{打印$ 1}'`
ogl_commands [统计] ='回声$ FTP_SEARCH | AWK'{
    对于(I = 6; I< = NF;我++)
        $打印我;
    }'`让数+ = 1完成< /tmp/ftp_search.14-12-02
12月1日23时59分03秒sslmftp1 ftpd的[4152]:USER XXXXXX
12月1日23时59分03秒sslmftp1 ftpd的[4152]:PASS密码
12月1日23时59分03秒sslmftp1 ftpd的[4152]:FTP登录FROM 172.19.x.xx [172.19.x.xx] XXXXXX
12月1日23时59分03秒sslmftp1 ftpd的[4152]:PWD
12月1日23时59分03秒sslmftp1 ftpd的[4152]:CWD /测试/数据/ 872507 /
12月1日23时59分03秒sslmftp1 ftpd的[4152]:TYPE Image`
12月1日23时59分03秒sslmftp1 ftpd的[4152]:PASV
12月1日23时59分04秒sslmftp1 ftpd的[4152]:NLST
12月1日23时59分04秒sslmftp1 ftpd的[4152]:FTP会议闭幕
12月1日23时59分05秒sslmftp1 ftpd的[4683]:USER XXXXXX
12月1日23时59分05秒sslmftp1 ftpd的[4683]:PASS密码
12月1日23时59分05秒sslmftp1 ftpd的[4683]:FTP登录FROM 172.19.1.24 [172.19.x.xx] XXXXXX
12月1日23时59分05秒sslmftp1 ftpd的[4683]:PWD
12月1日23时59分05秒sslmftp1 ftpd的[4683]:CWD /测试/数据/ 944837 /
12月1日23时59分05秒sslmftp1 ftpd的[4683]:输入图像


解决方案

  • 您并不需要保持一个迭代器添加到阵列。你可以简单地做阵列+ =(项目)不可以 阵列+ =项

  • 获取列在输入就像使用与多个目标变量一样简单。作为奖励,最后一个变量获得的第N个字的以及所有后续字的。参见帮助[R] EAD

这可以节省一吨的叉,但我没有测试它的速度有多快。

  ogl_date =()
[...]
ogl_commands =()而阅读-r日期1日期2时间服务器ID类型PID指令

    ogl_date + =($ $ DATE1 DATE2)
    [...]
    ogl_commands + =($指令)
完成< /tmp/ftp_search.14-12-02

I have a while loop that that reads in a ftp log file and puts it into an array so I'll be able to search through the array and match up/search for a flow. Unfortunately the while loop is taking forever to get through the file, it is a very large file but there must be another faster way of doing this.

# read file into array for original search results
while read FTP_SEARCH
do
ogl_date[count]=`echo $FTP_SEARCH | awk '{print $1, $2}'`
ogl_time[count]=`echo $FTP_SEARCH | awk '{print $3}'`
ogl_server[count]=`echo $FTP_SEARCH | awk '{print $4}'`
ogl_id[count]=`echo $FTP_SEARCH | awk '{print $5}'`
ogl_type[count]=`echo $FTP_SEARCH | awk -F '[' '{print $1}' | awk '{print $5}'`
ogl_pid[count]=`echo $FTP_SEARCH | awk -F'[' '{print $2}' | awk -F']' '{print $1}'`
ogl_commands[count]=`echo $FTP_SEARCH | awk '{
    for(i = 6; i <= NF; i++) 
        print $i;
    }'`

let "count += 1"

done < /tmp/ftp_search.14-12-02


Dec  1 23:59:03 sslmftp1 ftpd[4152]: USER xxxxxx  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: PASS password  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: FTP LOGIN FROM 172.19.x.xx [172.19.x.xx], xxxxxx  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: PWD  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: CWD /test/data/872507/  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: TYPE Image`
Dec  1 23:59:03 sslmftp1 ftpd[4152]: PASV
Dec  1 23:59:04 sslmftp1 ftpd[4152]: NLST
Dec  1 23:59:04 sslmftp1 ftpd[4152]: FTP session closed
Dec  1 23:59:05 sslmftp1 ftpd[4683]: USER xxxxxx 
Dec  1 23:59:05 sslmftp1 ftpd[4683]: PASS password
Dec  1 23:59:05 sslmftp1 ftpd[4683]: FTP LOGIN FROM 172.19.1.24 [172.19.x.xx], xxxxxx 
Dec  1 23:59:05 sslmftp1 ftpd[4683]: PWD
Dec  1 23:59:05 sslmftp1 ftpd[4683]: CWD /test/data/944837/
Dec  1 23:59:05 sslmftp1 ftpd[4683]: TYPE Image

解决方案

  • You don't need to keep an iterator to add to arrays. You can simply do array+=(item) (not array+=item).
  • Getting the columns in the input is as simple as using read with multiple target variables. As a bonus, the last variable gets the Nth word and all subsequent words. See help [r]ead.

This saves a ton of forks, but I haven't tested how fast it is.

ogl_date=()
[...]
ogl_commands=()

while read -r date1 date2 time server id type pid commands
do
    ogl_date+=("$date1 $date2")
    [...]
    ogl_commands+=("$commands")
done < /tmp/ftp_search.14-12-02

这篇关于bash脚本/ while循环非常慢读入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆