改进逻辑以使用awk在字符串中查找一组字符

时间:2014-05-22 04:10:08

标签: shell unix awk

专家, 我是UNIX的初学者。

我有一个文件NewFile.txt,其中包含以下内容:

DBLogFile.txt:Fri May 8 08:18:20 EDT 2014 (T76:Partition:User:PasswordAdapter:1uh8fl:buyerserver10) (jdbc:WARN) [ID6726]: Database connection pool size for database: Transaction.Schema1(DB details) (Login Param) exceeded 25 for connection Transaction.Schema1(DB details) (Login Param).  Current size is 26. Some text.
DBLogFile.txt:Fri May 16 8:18:43 EDT 2014 (T76:Partition:User:PasswordAdapter:y28z4v:buyerserver10) (jdbc:WARN) [ID6726]: Database connection pool size for database: Transaction.Schema1(DB details) (Login Param) exceeded 25 for connection Transaction.Schema1(DB details) (Login Param).  Current size is 26. Some text.
DBLogFile.txt:Fri May 8 8:18:46 EDT 2014 (T163:Partition:User:PasswordAdapter:o16tk5:buyerserver10) (jdbc:WARN) [ID6726]: Database connection pool size for database: Transaction.Schema1(DB details) (Login Param) exceeded 25 for connection Transaction.Schema1(DB details) (Login Param).  Current size is 26. Some text.
DBLogFile.txt:Fri May 16 08:18:46 EDT 2014 (T163:Partition:User:PasswordAdapter:o16tk5:buyerserver10) (jdbc:WARN) [ID6726]: Database connection pool size for database: Transaction.Schema1(DB details) (Login Param) exceeded 25 for connection Transaction.Schema1(DB details) (Login Param).  Current size is 26.
DBLogFile.txt:Fri May 16 08:18:47 EDT 2014 (T96:Partition:User:PasswordAdapter:wgnsvv:buyerserver10) (jdbc:WARN) [ID6726]: Database connection pool size for database: Transaction.Schema1(DB details) (Login Param) exceeded 25 for connection Transaction.Schema1(DB details) (Login Param).  Current size is 26.
DBLogFile.txt:Fri May 16 08:18:48 EDT 2014 (T96:Partition:User:PasswordAdapter:wgnsvv:buyerserver10) (jdbc:WARN) [ID6726]: Database connection pool size for database: Transaction.Schema1(DB details) (Login Param) exceeded 25 for connection Transaction.Schema1(DB details) (Login Param).  Current size is 26.
DBLogFile.txt:Fri May 16 08:19:19 EDT 2014 (T85:Partition:User:PasswordAdapter:ogdu87:buyerserver10) (jdbc:WARN) [ID6726]: Database connection pool size for database: Transaction.Schema1(DB details) (Login Param) exceeded 25 for connection Transaction.Schema1(DB details) (Login Param).  Current size is 26.
DBLogFile.txt:Fri May 16 08:19:48 EDT 2014 (T35:Partition:User:PasswordAdapter:ovfefb:buyerserver10) (jdbc:WARN) [ID6726]: Database connection pool size for database: Transaction.Schema1(DB details) (Login Param) exceeded 25 for connection Transaction.Schema1(DB details) (Login Param).  Current size is 26.

如果数据库连接数超过25并且显示当前大小,则日志基本上会发出警告。

我有以下代码片段来收集DBLogFileName(DBLogFile.txt),时间(Fri May 16 08:19:48 EDT 2014)和当前数据库大小(26

while read -r line; do 

    echo "...........";

    COUNTSTR=`echo $line | awk -F "Current size is " '{print $2}'`;

    DBCOUNT=`echo $COUNTSTR | awk '{ print substr($1,0,2) }'`;

    echo DBCOUNT is $DBCOUNT;
    echo DBCOUNT is $DBCOUNT >> $LOGFILE;

    NODE=`echo $line | awk -F "Log.txt:" '{print $1}'`;

    echo NODE is $NODE;
    echo NODE is $NODE >> $LOGFILE;

    WHEN=`echo $line | awk -F "Log.txt:" '{print substr($2,0,28)}'`;

    echo WHEN is $WHEN;
    echo WHEN is WHEN >> $LOGFILE;

    echo "...........";

done < NewFile.txt

我是否坚持使用awk的最佳做法?有没有办法改善这个? 我想收集:filename, time and size

1 个答案:

答案 0 :(得分:2)

让我们从这开始:

$ awk -v OFS=" - " '{
    split($1,a,/:/)
    name=a[1]
    time=a[2]" "$2" "$3" "$4" "$5" "$6
    sub(/.*Current size is /,"")
    size=$1+0
    print name, time, size
}' file
DBLogFile.txt - Fri May 8 08:18:20 EDT 2014 - 26
DBLogFile.txt - Fri May 16 8:18:43 EDT 2014 - 26
DBLogFile.txt - Fri May 8 8:18:46 EDT 2014 - 26
DBLogFile.txt - Fri May 16 08:18:46 EDT 2014 - 26
DBLogFile.txt - Fri May 16 08:18:47 EDT 2014 - 26
DBLogFile.txt - Fri May 16 08:18:48 EDT 2014 - 26
DBLogFile.txt - Fri May 16 08:19:19 EDT 2014 - 26
DBLogFile.txt - Fri May 16 08:19:48 EDT 2014 - 26

那很接近吗?还有什么需要让它完全符合你的要求?