【问题标题】:How can i print the data in tabular format in shell script?如何在 shell 脚本中以表格格式打印数据?
【发布时间】:2015-05-18 21:20:05
【问题描述】:

我正在编写一个需要以表格格式显示数据的脚本。 例如:从下面的数据来看,(我只是以表格的形式表示以便更好地理解)。我需要使用下面的列代码形成一个表格。如果特定唯一 ID 的代码值为 0,0,1,则为失败案例(例如:

17348514-1426250247230 has executed 3 times 0,0,1)
                                            F,F,S  will be considered as a failure case.


Year Mon D  Time Stamp     Unique ID                TT      Code
2015 Mar 13 09:48:16:099 -813380420-1426240092971  3.037     1
2015 Mar 13 09:54:47:525 -477148470-1426240484040  3.402     1
2015 Mar 13 11:26:42:728 1290473798-1426245992973  3.455     1
2015 Mar 13 12:37:27:338 17348514-1426250247230    0.008     0
2015 Mar 13 12:37:27:346 17348514-1426250247230    0.016     0
2015 Mar 13 12:37:27:354 17348514-1426250247230    0.024     1
2015 Mar 13 12:45:56:452 1124502614-1426250756375  0.009     0
2015 Mar 13 12:45:56:460 1124502614-1426250756375  0.017     0
2015 Mar 13 12:45:56:467 1124502614-1426250756375  0.024     1
2015 Mar 13 12:54:29:835 289692663-1426251267376   2.384     1
2015 Mar 13 13:24:53:226 2011983525-1426253085135  2.574     1
2015 Mar 13 14:28:13:076 -74229411-1426256890662   2.339     1

结果表应按以下格式打印。我不想打印完全成功,完全错误。下面一个是成功的。因此成功计数为1,错误计数为0。

Date      Hour  Time    UniqueID               Success   Error      timetaken
2015Mar13 14    14:28  -74229411-1426256890662   1          0         2.339 

From the first table,
2015 Mar 13 12:37:27:338 17348514-1426250247230    0.008     0
2015 Mar 13 12:37:27:346 17348514-1426250247230    0.016     0
2015 Mar 13 12:37:27:354 17348514-1426250247230    0.024     1

这是失败案例。所以需要打印的结果表格格式如下(添加到上面的行)

Date      Hour  Time    UniqueID               Success   Error      timetaken
2015Mar13 14    14:28  -74229411-1426256890662   1          0         2.339     
2015Mar13  12    12:37  17348514-1426250247230   0          1         0.024     

任何帮助将不胜感激

【问题讨论】:

  • 你介意使用 perl 吗? perl 的format 是您要求的最佳工具。

标签: bash shell awk sed substring


【解决方案1】:

TXR 语言:

Year Mon @(skip)
@(collect)
@year @month @day @hh:@mm:@ss:@msec @id @val @code
@  (collect :gap 0 :vars (aval))
@(skip) @id @aval @nil
@  (end)
@  (set code @(int-str code))
@  (bind total @(or [aval -1] val))
@(end)
@(output)
Date      Hour  Time    UniqueID                 Success   Error      timetaken
@  (repeat)
@year@month@{day 2} @hh    @hh:@mm   @{id 24} @{code 9} @{@(- 1 code) 10} @total
@  (end)
@(end)

运行:

$ txr reformat.txr data
Date      Hour  Time    UniqueID                 Success   Error      timetaken
2015Mar13 09    09:48   -813380420-1426240092971 1         0          3.037
2015Mar13 09    09:54   -477148470-1426240484040 1         0          3.402
2015Mar13 11    11:26   1290473798-1426245992973 1         0          3.455
2015Mar13 12    12:37   17348514-1426250247230   0         1          0.024
2015Mar13 12    12:45   1124502614-1426250756375 0         1          0.024
2015Mar13 12    12:54   289692663-1426251267376  1         0          2.384
2015Mar13 13    13:24   2011983525-1426253085135 1         0          2.574
2015Mar13 14    14:28   -74229411-1426256890662  1         0          2.339

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-11
    相关资源
    最近更新 更多