UNIX |使用awk从日志文件中提取特定值

时间:2017-09-05 10:52:25

标签: linux bash unix awk

我正在尝试从以下日志文​​件中提取特定值:

Table "xxx"."xxxx":

  3785568 Rows successfully loaded.
  0 Rows not loaded due to data errors.
  0 Rows not loaded because all WHEN clauses were failed.
  0 Rows not loaded because all fields were null.

Bind array size not used in direct path.
Column array  rows :    5000
Stream buffer bytes:  256000
Read   buffer bytes: 1048576

Total logical records skipped:          0
Total logical records read:       3785568
Total logical records rejected:         0
Total logical records discarded:        0
Total stream buffers loaded by SQL*Loader main thread:      878
Total stream buffers loaded by SQL*Loader load thread:      796

Run began on Fri Sep 01 04:00:26 2017
Run ended on Fri Sep 01 04:04:45 2017

Elapsed time was:     00:04:19.24
CPU time was:         00:00:08.56

我想要检索的是:

3785568 as number_rows
Sep 01 04:00:26 2017 as start_time
Sep 01 04:04:45 2017 as end_time 

这怎么可能用awk提取?

非常感谢任何帮助:)

非常感谢你的时间。

6 个答案:

答案 0 :(得分:1)

awk '/Rows successfully loaded/{
        print $1 " as number_rows"
        next
    }
    /Run began on/{ 
        sub(/Run began on /,""); 
        print $0 " as start_time"
        next 
   }
   /Run ended on/{
        sub(/Run ended on /,"");    
        print $0 " as end_time"
   }' infile

<强> 输入

$ cat infile
Table "xxx"."xxxx":

  3785568 Rows successfully loaded.
  0 Rows not loaded due to data errors.
  0 Rows not loaded because all WHEN clauses were failed.
  0 Rows not loaded because all fields were null.

Bind array size not used in direct path.
Column array  rows :    5000
Stream buffer bytes:  256000
Read   buffer bytes: 1048576

Total logical records skipped:          0
Total logical records read:       3785568
Total logical records rejected:         0
Total logical records discarded:        0
Total stream buffers loaded by SQL*Loader main thread:      878
Total stream buffers loaded by SQL*Loader load thread:      796

Run began on Fri Sep 01 04:00:26 2017
Run ended on Fri Sep 01 04:04:45 2017

Elapsed time was:     00:04:19.24
CPU time was:         00:00:08.56

<强> 输出

$ awk '/Rows successfully loaded/{
      print $1 " as number_rows"
      next
  }
  /Run began on/{ 
      sub(/Run began on /,""); 
      print $0 " as start_time"
      next 
  }
  /Run ended on/{
      sub(/Run ended on /,""); 
      print $0 " as end_time"
  }' infile

3785568 as number_rows
Fri Sep 01 04:00:26 2017 as start_time
Fri Sep 01 04:04:45 2017 as end_time

答案 1 :(得分:0)

因此,对于您的给定文件,这有效:

awk '/Rows/{ if (++n==1){ print $1 } }/began/ || /ended/{ print $5,$6,$7,$8 }' log.file

输出:

3785568
Sep 01 04:00:26 2017
Sep 01 04:04:45 2017

答案 2 :(得分:0)

为此目的,更好的解决方案是grep

ROWS=`grep "Total logical records read" logfile.txt | sed 's/[^0-9]*//g'` 
START=`grep "Run began on " | cut -d" " -f4-`

答案 3 :(得分:0)

如果您不介意 Perl grep -P

perl -lne 'print $& if /\d+ (?=Rows successfully)|^Run (began|ended) on Fri \K[^\n\r]+/g' file

输出:

3785568 
Sep 01 04:00:26 2017
Sep 01 04:04:45 2017

grep -Po '\d+ (?=Rows successfully)|^Run (began|ended) on Fri \K[^\n\r]+' file

答案 4 :(得分:0)

awk '/[[:digit:]]+[[:blank:]]Rows successfully/ { print $1" as number_rows" } /^Run began on .*$/ { print $4" "$5" "$6" "$7" "$8" as start_time" } /^Run ended on .*$/ { print $4" "$5" "$6" "$7" "$8" as end_time"}' filename

答案 5 :(得分:0)

awk 方法:

awk '/Rows success/{ print $1 }/^Run (began|ended)/{ print $5,$6,$7,$8 }' file

输出:

3785568
Sep 01 04:00:26 2017
Sep 01 04:04:45 2017