我需要根据分隔符将日志文件解析为多个文件

时间:2013-09-13 02:53:05

标签: bash file shell text-parsing fileparsing

我有一个日志文件,我需要将其解析为多个文件。

############################################################################################
6610
############################################################################################
GTI02152 I    gtirreqi 20130906 000034 TC SJ014825         GTT_E_REQ_INF テーブル挿入件数 16件

############################################################################################
Z5000
############################################################################################
GTP10000 I NIPS     gtgZ5000 20130906 000054 TC SJ014825         シェル開始
############################################################################################

我需要创建像6610.txt这样的文件,它们的所有值分别为6610(GTI02152 ..)和z5000(GTP10000)。任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:1)

下面的脚本可以帮助您获取信息。您可以修改它们以创建所需的数据。

#!/bin/sh

cmd=`cat data.dat | paste -d, - - - - - | cut -d ',' -f 2,4 > file.out`
$cmd

while read p; do
    fileName=`echo $p | cut -d ',' -f 1`
    echo $fileName
    dataInfo=`echo $p | cut -d ',' -f 2`
    echo $dataInfo
done< file.out

答案 1 :(得分:0)

这是一个awk风格的答案:

我将以下内容放入名为awkochmod +x的文件中以使用它:

#!/usr/bin/awk -f

BEGIN { p = 0 }    # look for filename flag - start at zero

/^\#/ { p = !p }   # turn it on to find the filename

    # either make a filename or write to the last filename based on the flag
$0 !~ /^\#/ {
    if( p == 1 ) filename = $1 ".txt"
    else print $0 > filename
    }

正在运行awko data.txt会从您的示例数据中生成两个文件6610.txtZ5000.txt。它还能够向输出文件发送更多数据行。

答案 2 :(得分:0)

你也可以用Ruby做到这一点:

ruby -e 'File.read(ARGV.shift).scan(/^[^#].*?(?=^[#])/m).each{|e| name = e.split[0]; File.write("#{name}.txt", e)}' file

示例输出:

> for A in *.txt; do echo "---- $A ----"; cat "$A"; done
---- 6610.txt ----
6610
---- GTI02152.txt ----
GTI02152 I    gtirreqi 20130906 000034 TC SJ014825         GTT_E_REQ_INF テーブル挿入件数 16件

---- GTP10000.txt ----
GTP10000 I NIPS     gtgZ5000 20130906 000054 TC SJ014825         シェル開始
---- Z5000.txt ----
Z5000

答案 3 :(得分:0)

此脚本做出以下假设:

  • 每条记录用空行分隔
  • ####行是纯注释/空格填充,在解析期间可以忽略
  • 每条记录的第一行(忽略####)包含文件名的基本名称

日志文件的名称作为此脚本的第一个参数传递。

#!/bin/bash

# write records to this temporary file, rename later
tempfile=$(mktemp)
while read line; do
    if [[ $line == "" ]] ; then
        # line is empty - separator - save existing record and start a new one
        mv $tempfile $filename
        filename=""
        tempfile=$(mktemp)
    else
        # output non-empty line to record file
        echo $line >> $tempfile
        if [[ $filename == "" ]] ; then
            # we haven't yet figured out the filename for this record
            if [[ $line =~ ^#+$ ]] ; then
                # ignore #### comment lines
                :
            else
                # 1st non-comment line in record is filename
                filename=${line}.txt
            fi
        fi
    fi
done < $1
# end of input file might not have explicit empty line separator -
# make sure last record file is moved correctly
if [[ -e $tempfile ]] ; then
    mv $tempfile $filename
fi
相关问题