我如何在Shell中获得MCCLI输出中的定界符

发布于 2025-01-18 05:13:22 字数 1493 浏览 1 评论 0原文

在尝试处理“mccli Activity show”的输出(例如)时,输出可能如下所示:

ID               Status                   Error Code Start Time           Elapsed     End Time             Type             Progress Bytes    New Bytes    

---------------- ------------------------ ---------- -------------------- ----------- -------------------- ---------------- ----------------- ---------    

9133910640004809 Completed w/Exception(s) 10020      2012-06-07 18:00 EDT 00h:53m:46s 2012-06-07 18:53 EDT Scheduled Backup 215   0.2%

9133914600006909 Completed                0          2012-06-08 05:00 EDT 00h:00m:04s 2012-06-08 05:00 EDT Scheduled Backup 0                 0%

Desired Output

ID,               Status,                   Error Code, Start Time          ,Elapsed     ,End Time             ,Type             ,Progress Bytes    ,New Bytes    

----------------,------------------------, ---------- ,--------------------, -----------, --------------------, ---------------- ,----------------- ,---------    

9133910640004809, Completed w/Exception(s), 10020,      2012-06-07 18:00 EDT, 00h:53m:46s ,2012-06-07 18:53 EDT, Scheduled Backup,  215 ,  0.2%

9133914600006909, Completed ,               0,          ,2012-06-08 05:00 EDT, 00h:00m:04s ,2012-06-08 05:00 EDT, Scheduled Backup, 0 ,                0%

Code I Tried

awk '!/---/{$1=x; sub(/,+$/,x)}1' OFS=,

but it does not work as it put comma for every space

In trying to process the output of "mccli activity show" (for example), the output might be like this:

ID               Status                   Error Code Start Time           Elapsed     End Time             Type             Progress Bytes    New Bytes    

---------------- ------------------------ ---------- -------------------- ----------- -------------------- ---------------- ----------------- ---------    

9133910640004809 Completed w/Exception(s) 10020      2012-06-07 18:00 EDT 00h:53m:46s 2012-06-07 18:53 EDT Scheduled Backup 215   0.2%

9133914600006909 Completed                0          2012-06-08 05:00 EDT 00h:00m:04s 2012-06-08 05:00 EDT Scheduled Backup 0                 0%

Desired Output

ID,               Status,                   Error Code, Start Time          ,Elapsed     ,End Time             ,Type             ,Progress Bytes    ,New Bytes    

----------------,------------------------, ---------- ,--------------------, -----------, --------------------, ---------------- ,----------------- ,---------    

9133910640004809, Completed w/Exception(s), 10020,      2012-06-07 18:00 EDT, 00h:53m:46s ,2012-06-07 18:53 EDT, Scheduled Backup,  215 ,  0.2%

9133914600006909, Completed ,               0,          ,2012-06-08 05:00 EDT, 00h:00m:04s ,2012-06-08 05:00 EDT, Scheduled Backup, 0 ,                0%

Code I Tried

awk '!/---/{$1=x; sub(/,+$/,x)}1' OFS=,

but its not working as its putting comma for every space

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风透绣罗衣 2025-01-25 05:13:22

您发布的预期输出并没有真正的意义(例如,一个明显字段的0,的明显字段的逗号,有时您在逗号之前,有时在逗号之前,有时在其之前和之后都有空白)和我认为您的示例输入不是准确的(例如2150.2%看起来太近了),所以我猜是 - 但是 - 使用GNU AWK用于fiedwidths < /code>和nextfile,这可能是您真正想做的:

$ cat tst.awk
NR == FNR {
    if ( /^[- ]+$/ ) {
        wids = length($1)
        for ( i=2; i<=NF; i++ ) {
            wids = wids " " length(FS $i)
        }
        nf = NF
        nextfile
    }
    next
}
FNR == 1 {
    FIELDWIDTHS = wids
    OFS = ","
    $0 = $0
}
NF { $nf = $nf }
{ print }

$ awk -f tst.awk file file
ID              , Status                  , Error Code, Start Time          , Elapsed    , End Time            , Type            , Progress Bytes   , New Bytes

----------------, ------------------------, ----------, --------------------, -----------, --------------------, ----------------, -----------------, ---------

9133910640004809, Completed w/Exception(s), 10020     , 2012-06-07 18:00 EDT, 00h:53m:46s, 2012-06-07 18:53 EDT, Scheduled Backup, 215   0.2%,

9133914600006909, Completed               , 0         , 2012-06-08 05:00 EDT, 00h:00m:04s, 2012-06-08 05:00 EDT, Scheduled Backup, 0                , 0%

或者:

$ cat tst.awk
NR == FNR {
    if ( /^[- ]+$/ ) {
        wids = length($1)
        for ( i=2; i<=NF; i++ ) {
            wids = wids " " length(FS $i)
        }
        nf = NF
        nextfile
    }
    next
}
FNR == 1 {
    FIELDWIDTHS = wids
    OFS = ","
    $0 = $0
}
NF {
    $nf = $nf
    $0 = gensub(/( +),/,",\\1","g")
}
{ print }

$ awk -f tst.awk file file
ID,               Status,                   Error Code, Start Time,           Elapsed,     End Time,             Type,             Progress Bytes,    New Bytes

----------------, ------------------------, ----------, --------------------, -----------, --------------------, ----------------, -----------------, ---------

9133910640004809, Completed w/Exception(s), 10020,      2012-06-07 18:00 EDT, 00h:53m:46s, 2012-06-07 18:53 EDT, Scheduled Backup, 215   0.2%,

9133914600006909, Completed,                0,          2012-06-08 05:00 EDT, 00h:00m:04s, 2012-06-08 05:00 EDT, Scheduled Backup, 0,                 0%

You posted expected output doesn't really make sense (e.g. 2 commas for one apparent field at 0, , and sometimes you have blanks before the comma, sometimes after it, and sometimes before AND after it) and I think your sample input isn't accurate (e.g. 215 and 0.2% appears too close together) so I'm guessing but - using GNU awk for FIEDWIDTHS and nextfile, this may be what you really want to do:

$ cat tst.awk
NR == FNR {
    if ( /^[- ]+$/ ) {
        wids = length($1)
        for ( i=2; i<=NF; i++ ) {
            wids = wids " " length(FS $i)
        }
        nf = NF
        nextfile
    }
    next
}
FNR == 1 {
    FIELDWIDTHS = wids
    OFS = ","
    $0 = $0
}
NF { $nf = $nf }
{ print }

$ awk -f tst.awk file file
ID              , Status                  , Error Code, Start Time          , Elapsed    , End Time            , Type            , Progress Bytes   , New Bytes

----------------, ------------------------, ----------, --------------------, -----------, --------------------, ----------------, -----------------, ---------

9133910640004809, Completed w/Exception(s), 10020     , 2012-06-07 18:00 EDT, 00h:53m:46s, 2012-06-07 18:53 EDT, Scheduled Backup, 215   0.2%,

9133914600006909, Completed               , 0         , 2012-06-08 05:00 EDT, 00h:00m:04s, 2012-06-08 05:00 EDT, Scheduled Backup, 0                , 0%

or:

$ cat tst.awk
NR == FNR {
    if ( /^[- ]+$/ ) {
        wids = length($1)
        for ( i=2; i<=NF; i++ ) {
            wids = wids " " length(FS $i)
        }
        nf = NF
        nextfile
    }
    next
}
FNR == 1 {
    FIELDWIDTHS = wids
    OFS = ","
    $0 = $0
}
NF {
    $nf = $nf
    $0 = gensub(/( +),/,",\\1","g")
}
{ print }

$ awk -f tst.awk file file
ID,               Status,                   Error Code, Start Time,           Elapsed,     End Time,             Type,             Progress Bytes,    New Bytes

----------------, ------------------------, ----------, --------------------, -----------, --------------------, ----------------, -----------------, ---------

9133910640004809, Completed w/Exception(s), 10020,      2012-06-07 18:00 EDT, 00h:53m:46s, 2012-06-07 18:53 EDT, Scheduled Backup, 215   0.2%,

9133914600006909, Completed,                0,          2012-06-08 05:00 EDT, 00h:00m:04s, 2012-06-08 05:00 EDT, Scheduled Backup, 0,                 0%
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文