使用AWK格式化数据输出

时间:2016-01-24 14:44:57

标签: bash awk gawk

你能帮助我使用awk扫描这个Control-m输出吗? 这是一个Control-m输出,我需要格式化它。

ORDERID  JOBNAME           TYPE  ODATE    STATE     STATUS FROMTIME UNTIL
-------- ----------------- ----  -------  -------   ------ -------- -----
0002daew AA60AMBI25        JOB   20160104 Wait Con
0002db84 MA34PICT01        JOB   20160105 Post pro OK
0002dc0j OPIPMJD040W       JOB   20160105 Wait Tim        1000                Deleted
0002dbw6 TR60ADCR05        JOB   20160105 Post pro OK
0002de14 WKIPEJD007W       JOB   20160106 Wait Tim NOTOK
0002dbwc OU60ADMK12        JOB   20160105 Post pro OK     0800
0002dbwd LO60LC0012        CMD   20160105 Post pro OK
0002dcxc TYCCAJD001P       JOB   20160106 Executin        2200     0500
0002dbwe OAT0ADMK13        JOB   20160105 Post pro OK     1800     0800
0002dbwf DAT60ADMK14       JOB   20160105 Post pro OK     1800     0800
0002dbxs CR60AMBI24        JOB   20160105 Post pro OK
0002dbyz LQ60ADBI24        JOB   20160105 Post pro OK

并创建此输出:

JOBNAME           STATUS     STATE      ODATE      FROM       UNTIL
----------------  ------     -------    -------    ----       ----
AA60AMBI25                   Wait Con   20160104
MA34PICT01       OK          Post pro   20160105
OPIPMJD040W      Deleted     Wait Tim   20160105   10:00
TR60ADCR05       OK          Post pro   20160105
WKIPEJD007W      NOTOK       Wait Tim   20160106
OU60ADMK12       OK          Post pro   20160105   08:00
LO60LC0012       OK          Post pro   20160105
TYCCAJD001P                  Executin   20160106   22:00       05:00
OAT0ADMK13       OK          Post pro   20160105   18:00       08:00
DAT60ADMK14      OK          Post pro   20160105   18:00       08:00
CR60AMBI24       OK          Post pro   20160105
LQ60ADBI24       OK          Post pro   20160105

1 个答案:

答案 0 :(得分:2)

您可以这样做:

awk 'BEGIN { FIELDWIDTHS = "9 18 6 9 9 7 9 9"}  {$0 = sprintf("%100-s\n", $0); print $2 $6 " "  $5 $4 $7 $8}' cm.txt

哪里

  • ' cm.txt'是你的文件
  • FIELDWIDTHS列出不同列的长度,如果是固定宽度列(例如,当没有合适的分隔符时)
  • sprintf命令将每一行扩展为100个字符,以便以下打印看起来可以用于短行

留给你:

  • 使用:添加一个格式化时间的函数(您的所需输出具有此转换)
  • 在您的示例中指定如何处理Deleted的orderid 0002dc0jstatus