How to speed up this log parser?

I have a large log file with gigabytes in this format:

2016-02-26 08:06:45 Blah blah blah

I have a log parser that splits the log of a single file into separate files by date, trimming the date from the original string.

I need a form teeso that I can see how far the process has come.

The problem is that this method is breathtakingly slow. Is there no way to do this fast in bash? Or will I have to hack a small C program to do this?

log_file=server.log
log_folder=logs

mkdir $log_folder 2> /dev/null

while read a; do
   date=${a:0:10}

   echo "${a:11}" | tee -a $log_folder/$date
done < <(cat $log_file)
0
source share
2 answers

awk- - - - - , , - ​​ "0000-00-00" ,

dir=$1
if [[ -z $dir ]]; then
  echo >&2 "Usage: $0 outdir <logfile"
  echo >&2 "outdir: directory where output files are created"
  echo >&2 "logfile: input on stdin to split into output files"
  exit 1
fi
mkdir -p $dir
echo "output directory \"$dir\""
awk -vdir=$dir '
BEGIN {
  datepat="[0-9]{4}-[0-9]{2}-[0-9]{2}"
  date="0000-00-00"
  file=dir"/"date
}
date != $1 && $1 ~ datepat {
  if(file) {
    close(file)
    print ""
  }
  print $1 ":"
  date=$1
  file=dir"/"date
}
{
  if($1 ~ datepat)
    line=substr($0,12)
  else
    line=$0
  print line
  print line >file
}
'
head -6 $dir/*

first line without date
2016-02-26 08:06:45 0 Blah blah blah
2016-02-26 09:06:45 1 Blah blah blah
2016-02-27 07:06:45 2 Blah blah blah
2016-02-27 08:06:45 3 Blah blah blah
no date line
blank lines

another no date line
2016-02-28 07:06:45 4 Blah blah blah
2016-02-28 08:06:45 5 Blah blah blah

first line without date

2016-02-26:
08:06:45 0 Blah blah blah
09:06:45 1 Blah blah blah

2016-02-27:
07:06:45 2 Blah blah blah
08:06:45 3 Blah blah blah
no date line
blank lines

another no date line

2016-02-28:
07:06:45 4 Blah blah blah
08:06:45 5 Blah blah blah

==> tmpd/0000-00-00 <==
first line without date

==> tmpd/2016-02-26 <==
08:06:45 0 Blah blah blah
09:06:45 1 Blah blah blah

==> tmpd/2016-02-27 <==
07:06:45 2 Blah blah blah
08:06:45 3 Blah blah blah
no date line
blank lines

another no date line

==> tmpd/2016-02-28 <==
07:06:45 4 Blah blah blah
08:06:45 5 Blah blah blah
+2

read bash . , , , awk:

#!/bin/bash

log_file=input
log_directory=${1-logs}

mkdir -p $log_directory

awk 'NF>1{d=l"/"$1; $1=""; print > d}' l=$log_directory $log_file

stdout, , tty, . :

awk '{d=l"/"$1; $1=""; print > d}1' l=$log_directory $log_file

( "1" .)

+3

Source: https://habr.com/ru/post/1666947/


All Articles