-1

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 "%02d" 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 "%02d" 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
YYZZYY
  • 31
  • 5
  • 2
    Your syntax seems bewilderingly wrong in many other ways too. Can you show the entire script and perhaps how you are trying to run it? – tripleee Sep 17 '21 at 12:01
  • 4
    The statements in your question that you say are in file `error.awk` are neither awk nor shell and then you say you want to add a `0` to a number but show expected output that looks like `2300-2400` . Please create and post a [mcve] with concise, testable sample input, expected output and syntactically correct code for whatever tool you're trying to use to solve the problem yourself. – Ed Morton Sep 17 '21 at 12:36
  • Your other questions answer doesn't matter, make sure **this question** makes sense standalone. FWIW though, even after reading that other questions answer the code in your question still doesn't make any sense and is still neither shell nor awk. – Ed Morton Sep 17 '21 at 16:38
  • Pinging those who commented here wholesale to get help with a new question is borderline abusive. Please don't do that. – tripleee Sep 17 '21 at 16:47
  • @EdMorton I have updated the details of the question – YYZZYY Sep 17 '21 at 16:48
  • @tripleeet No, I just want to explain this problem as best as I can, instead of asking new questions. The calling method in this question is derived from the answer to the previous question and is for reference only. I'm sorry if it offends. – YYZZYY Sep 17 '21 at 16:51
  • It looks like you're confusing shell syntax and awk syntax – Shawn Sep 17 '21 at 17:22
  • @EdMorton updated again – YYZZYY Sep 17 '21 at 17:27
  • You're definitely getting confused about shell and awk. Shell and awk are 2 completely different tools. Just because you can call awk from shell doesn't mean you can do whatever else you do in shell within awk, any more than you could use shell constructs within a C program. You say `Enter the following text into the variable trimline in x.sh` so does that mean that `trimline` is a shell variable containing those 4 lines and so `trimline | awk` should really be `echo "$trimline" | awk` ? – Ed Morton Sep 17 '21 at 17:39
  • @EdMortonE My bad, trimline is a function that uses awk to read and organize text files and perform simple operations, such as removing spaces at the beginning of lines. The input in the description is part of the function output value after finishing – YYZZYY Sep 17 '21 at 17:57
  • OK, sounds like none of that matters to the question you're aking then and so you should remove that from your question and just ask about running an awk script on the input you provided. – Ed Morton Sep 17 '21 at 18:03
  • I still can't figure out what you're question is about even given that clarification though. Please try again to come up with a [mcve] that has concise, testable sample input, expected output, and code that's JUST about whatever it is you're asking for help with. – Ed Morton Sep 17 '21 at 18:06
  • Btw, never do `printf foo` for any `foo` that can contain input data (e.g. `printf "description:" description` and others) as it'll fail cryptically when `foo` contains `printf` formatting characters like `%s`, do `printf "%s", foo` instead. – Ed Morton Sep 17 '21 at 18:10

1 Answers1

0

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("%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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185