【问题标题】:convert syslog file into csv将 syslog 文件转换为 csv
【发布时间】:2021-09-11 05:28:09
【问题描述】:

$cat syslog* | grep -a "CRON" | grep "CMD"

Jun 24 22:17:01 ubuntu1804-template CRON[7772]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)  
Jun 24 23:17:01 ubuntu1804-template CRON[7779]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)  
2021-06-24T10:05:01.590200-07:00 ubuntu1804-template CRON[7772]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)  
2021-06-25T10:05:01.590200-07:00 ubuntu1804-template CRON[7772]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)  

我想将其转换为具有三列的 csv 文件。正在从 variable 获取 Hostname
预期输出:

Date                                 Hostname             Message  
Jun 24 22:17:01                      $variable                     CMD (   cd / && run-parts --report /etc/cron.hourly)  
Jun 24 23:17:01                      $variable                     CMD (   cd / && run-parts --report /etc/cron.hourly)  
2021-06-24T10:05:01.590200-07:00     $variable                     CMD (   cd / && run-parts --report /etc/cron.hourly)  
2021-06-25T10:05:01.590200-07:00     $variable                     CMD (   cd / && run-parts --report /etc/cron.hourly)  

我正在尝试使用 awk,但日期格式会影响输出

【问题讨论】:

    标签: bash shell csv syslog


    【解决方案1】:

    试试这个:

    # set in you need
    variable='ubuntu1804-template'
    
    cat syslog* | grep -a "CRON" | grep "CMD" | awk -F"$variable" -vhostname="$variable" 'BEGIN {printf("%-40s %-30s %s\n", "Date","Hostname","Message")} {split($NF, rstArr, "CMD"); printf("%-40s %-30s %s\n", $1, hostname, "CMD"rstArr[2])}'
    

    你可以用printf("%-40s %-30s %s\n", $1..)格式化你的输出,这个%-40s %-30s %s\n将定义行格式。

    【讨论】:

      猜你喜欢
      • 2021-06-06
      • 2012-02-16
      • 2018-01-29
      • 2011-03-05
      • 2015-10-28
      • 2014-11-22
      • 2017-04-18
      • 2013-04-21
      • 2018-02-25
      相关资源
      最近更新 更多