【问题标题】:How to add 0 to less than two digits in awk script [closed]如何在 awk 脚本中将 0 添加到小于两位数 [关闭]
【发布时间】:2021-11-12 06:54:41
【问题描述】:

输入:在x.sh的函数trimline中输入以下文本

May 13 00:01:58 BBAOMACBOOKAIR2 com.apple.xpc.launchd[1] (com.apple.mdworker.bundles[12513]): Service exited with abnormal code: 78
May 13 00:02:12 BBAOMACBOOKAIR2 com.apple.xpc.launchd[1] (com.apple.xpc.launchd.domain.pid.mdmclient.12523): Failed to bootstrap path: path = /usr/libexec/mdmclient, error = 108: Invalid path
May 13 00:04:20 BBAOMACBOOKAIR2 syslogd[113]: ASL Sender Statistics
May 13 00:05:58 BBAOMACBOOKAIR2 com.apple.xpc.launchd[1] (com.apple.mdworker.bundles[12535]): Could not find uid associated with service: 0: Undefined error: 0 501

我在x.sh文件中使用如下语句调用error.awk文件来实现我想要的输出。

文件:x.sh

trimline| awk -f error.awk

文件:error.awk

{
    gsub(/^ */, "", $0)
    gsub(/ *$/, "", $0)
    FS = ":"
    if ($5 == "") {
        next
    }
}
{
    FS = " "
    deviceName = $4
    processId = $5
    gsub(/^.*\[/, "", processId)
    gsub(/\].*$/, "", processId)
    processName = $5
    $1 = $2 = $3 = $4 = $5 = ""
    gsub(/^ */, "", $0)
    description = $0
    trimtime = $3
    FS = ":"
    time = $1
   #timeplus=$(echo "$time+1"|bc)
    timeplus=let $time + 1
    timeadd='{ printf "%02d" timeplus }'
    timeWindow=$time"00_"$timeadd"00"

    printf "deviceName:" deviceName
    printf "processId:" processId
    printf "processName:" processName
    printf "description:" description
    printf "timeWindow:" timeWindow
}

但是下一句报语法错误,这句话的目的是将文本中的时间重新排列成一个时间段然后输出,比如00:01:58转换为0000-0100,原理是以小时为单位取值

timeadd='{ printf "%02d" timeplus }'

问题是如何修正这个语句,作用是在只有一位数的时候左加0。

处理流程是将冒号隔开的第一项的值取出加1,再将0加1。

       step 1 00:01:58
       step 2 00
       step 3 00+1
       step 4 01    

我想要 timeWindow 输出的是时间间隔:

0000-0100
0100-0200
0300-0400
...
2300-2400

【问题讨论】:

  • 您的语法在许多其他方面似乎也存在令人眼花缭乱的错误。您能否展示整个脚本以及您尝试运行它的方式?
  • 您在文件error.awk 中所说的问题中的语句既不是awk 也不是shell,然后您说您想将0 添加到一个数字但显示看起来像@ 的预期输出987654330@ 。请创建并发布minimal reproducible example,其中包含简洁、可测试的示例输入、预期输出和语法正确的代码,适用于您尝试使用自己解决问题的任何工具。
  • 您的其他问题的答案无关紧要,请确保这个问题独立有意义。 FWIW,即使在阅读了其他问题之后回答您问题中的代码仍然没有任何意义,并且仍然既不是 shell 也不是 awk。
  • ping 那些在这里评论的人以获取新问题的帮助是边缘滥用。请不要那样做。
  • @EdMorton 我已经更新了问题的详细信息

标签: shell awk


【解决方案1】:

这是你想要做的(基于my answer to your previous question)吗?

$ cat tst.sh
#!/usr/bin/env bash

# Using "cat file" in place of "trimline" which I dont have.
cat file |
awk '
    { split($0,errChk,/:/) }

    errChk[5] != "" {
        split($3,t,/:/)
        timeRange = sprintf("%02d00-%02d00",t[1],t[1]+1)
        deviceName = $4
        processId = processName = $5
        gsub(/.*\[|].*/,"",processId)
        description = $0
        sub(/[^(]*/,"",description)

        print timeRange
        print deviceName
        print processId
        print processName
        print description
    }
'

$ ./tst.sh
0000-0100
BBAOMACBOOKAIR2
1
com.apple.xpc.launchd[1]
(com.apple.mdworker.bundles[12513]): Could not find uid associated with service: 0: Undefined error: 0 501
0000-0100
BBAOMACBOOKAIR2
1
com.apple.xpc.launchd[1]
(com.apple.mdworker.bundles[12513]): Service exited with abnormal code: 78
0000-0100
BBAOMACBOOKAIR2
1
com.apple.xpc.launchd[1]
(com.apple.xpc.launchd.domain.pid.mdmclient.12523): Failed to bootstrap path: path = /usr/libexec/mdmclient, error = 108: Invalid path

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-24
    • 2021-11-30
    • 2019-07-27
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多