Home > Blockchain >  How to add 0 to less than two digits in awk script [closed]
How to add 0 to less than two digits in awk script [closed]

Time:09-21

Input: Enter the following text into the function trimline in x.sh

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

I use the following statement in the x.sh file to call error.awk file to achieve the output I want.

file: x.sh

trimline| awk -f error.awk

file: 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 "d" timeplus }'
    timeWindow=$time"00_"$timeadd"00"

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

But the next sentence reports a syntax error,The purpose of this sentence is to rearrange the time in the text into a time period and then output, such as 00:01:58 converted to 0000-0100, the principle is to take the value in hours

timeadd='{ printf "d" timeplus }'

The question is how to fix this statement, the function is to add 0 to the left when there is only one digit.

The processing flow is to take out the value of the first item separated by a colon and add 1,and add 0 to 1.

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

What I want the timeWindow output is time interval:

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

CodePudding user response:

Is this what you're trying to do (building upon 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("d00-d00",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
  • Related