Bashを使用してログファイルをCSVに変換する方法

Bashを使用してログファイルをCSVに変換する方法

sed / awkを使用してこのログエントリを正しい形式のcsvファイルに変換できますか?私はsed / awkを使ってこれを行ういくつかの方法を試しました。残念ながら私はできません。 Perlでやる前に。 AWK / sedまたはbashでもこれは可能ですか?

Jan 21 11:10:45 hostname.internal.nl THREAT 2015/01/21 11:10:44 critical 5 reset-both outside 10.10.10.10 inside 10.16.61.4 tcp 39898 80 web-browsing any Bash Remote Code Execution Vulnerability(36731)

このように

Date  | Hostname | Threat | DATE+time | Critical/High | Count | --- | External IP | Internal IP | TCP/UDP | Port | External Port| Category | Vulnerability 

Jan 21 11:10:45 | hostname.internal.nl | THREAT | 2015/01/21 11:10:44 | critical 5 reset-both | outside 10.10.10.10 | inside 10.16.61.4 | tcp  39898 | 80 | web-browsing | 4any Bash Remote Code Execution Vulnerability(36731)

ベストアンサー1

はい、Bashでこれを行うことができますが、なぜそれを行うのかわかりません。純粋なbashソリューションは次のとおりです。

$ while read -r mon day time host threat date time crit count sugg out exip \
                in inip tcp port export cat vuln; do 
     printf "%s | " "$mon $day $time" "$host" "$threat" "$date $time" \ 
                    "$crit $count $sugg" "$out $exip" "$in $inip" "$tcp \
                    $port" "$export" "$cat" "$vuln"
  done < file; printf "\n"
Jan 21 11:10:44 | hostname.internal.nl | THREAT | 2015/01/21 11:10:44 | critical 5 reset-both | outside 10.10.10.10 | inside 10.16.61.4 | tcp 39898 | 80 | web-browsing | any Bash Remote Code Execution Vulnerability(36731) | 

Perlまたはawkでより良い方法を提供できますが、フィールドをどれだけ正確に定義するかによって異なります。質問をより詳細に更新したら(たとえば、キーフィールドには常に3つの単語がありますか?入力で常に同じものが何であるか、何が変更できるかを知る必要があります)、より良いソリューションを提供できます。

おすすめ記事