简体   繁体   English

将linux日志转换为csv

[英]Convert linux log into csv

I'm newbie on linux. 我是Linux上的新手。 I'm so sorry for asking this question again. 很抱歉再次提出这个问题。 But I am really appreciate if someone could help me on this. 但我真的很感激有人能帮助我。 I have trouble on how to convert my linux log to csv file for more readable. 我在如何将Linux日志转换为csv文件时遇到麻烦,以提高可读性。

I have apache log as bellow: 我有下面的Apache日志:

[Sun Mar 01 06:01:30 2015] [error] [client 123.456.789.012] File does not exist: /var/www/html/

How can I separate them by column, using: Date ( Sun Mar 01 06:01:30 2015 ), IP ( 123.456.789.012 ) only IP, Error Message ( File does not exist ) and Target ( /var/www/html/ )? 如何使用列按日期分开它们:日期( 2015年3月1日06:01:30 ),仅IP( 123.456.789.012 ),IP,错误消息( 文件不存在 )和目标( / var / www / html / )?

Thank you 谢谢

There are many ways t achieve it in shell script. 在shell脚本中有很多方法可以实现它。 Will describe the method in detail and will give a sample example. 将详细描述该方法并给出示例示例。

You have to identify the delimiter to partition your string and either you can use awk or sed command to partition the fields according to the delimiter 您必须标识分隔符以对字符串进行分区,并且可以使用awk或sed命令根据分隔符对字段进行分区

For example in you case you can consider ']' as delimiter s to break the line using the delimiter command will be as follows: 例如,在您可以将']'用作分隔符的情况下,使用delimiter命令将其换行,如下所示:

cat logfile | awk -F']' '{print "$1, $2, $3"}' > new_log_file.csv

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM