-2
confirmerror(){
trimline|awk -F ':' '{if($5 != "") print}'
}
result(){
deviceName=$($1|awk '{print $4}')
processId=$($1|awk '{print $5}'|awk -F '[^0-9]*' '{print $2}')
processName=$($1|awk '{print $5}')
description=$($1|awk '{print $6,$7,$8,$9}')
echo $deviceName
echo $processId
echo $processName
echo $description
}
confirmerror | while read line; do result $line; done

The OUTPUT of function confirmerror:

May 13 00:01:58 BBAOMACBOOKAIR2 com.apple.xpc.launchd[1] (com.apple.mdworker.bundles[12513]): Could not find uid associated with service: 0: Undefined error: 0 501
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

Need to read each line of function x, after processing, get the corresponding format output, the top is my attempt, but it failed, so I come to seek advice.

What I want:

BBAOMACBOOKAIR2 
1
com.apple.xpc.launchd[1]
(com.apple.mdworker.bundles[12513]): Could not find uid associated with service: 0: Undefined error: 0 501
BBAOMACBOOKAIR2
1
com.apple.xpc.launchd[1]
(com.apple.mdworker.bundles[12513]): Service exited with abnormal code: 78
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
YYZZYY
  • 31
  • 5
  • `while read line; do confirmerror $(result $line); done` – Daniel Hornik Sep 16 '21 at 17:31
  • @DanielHornik not working – YYZZYY Sep 16 '21 at 19:38
  • Need to be completed in the form of a function, not a separate awk file. – YYZZYY Sep 17 '21 at 03:45
  • 1
    Don't use a shell read loop to manipulate text, see [why-is-using-a-shell-loop-to-process-text-considered-bad-practice](https://unix.stackexchange.com/questions/169716/why-is-using-a-shell-loop-to-process-text-considered-bad-practice). Also, you have multiple shell errors in your script, copy/paste that and any other shell scripts you write until you get the fundamentals figured out into http://shellcheck.net and fix the basic issues it tells you about. – Ed Morton Sep 17 '21 at 16:45
  • There's no sample input in your question and you're calling a tool or function named `trimline` that you don't define, so ikd how we can help you with your problem. The right solution will not be a shell script calling awk multiple times. Try again and tag your question with `awk`. – Ed Morton Sep 17 '21 at 16:47
  • `$($1|awk ...)` is a common beginner syntax error; duplicate of https://stackoverflow.com/questions/4775548/how-to-pass-the-value-of-a-variable-to-the-stdin-of-a-command – tripleee Sep 17 '21 at 16:53
  • ... But I agree, this angry bitter fruit salad of a shell script will turn into a calm and sweet little dessert if you refactor all of it to an Awk script. – tripleee Sep 17 '21 at 16:56

2 Answers2

1

I think, is too complicated for the job. You have awk, so make the job with it.

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
    print deviceName
    print processId
    print processName
    print description
}

And use it:

output_command_or_cat_filename | awk -f error.awk

Output:

BBAOMACBOOKAIR2
1
com.apple.xpc.launchd[1]
(com.apple.mdworker.bundles[12513]): Could not find uid associated with service: 0: Undefined error: 0 501
BBAOMACBOOKAIR2
1
com.apple.xpc.launchd[1]
(com.apple.mdworker.bundles[12513]): Service exited with abnormal code: 78
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
Arnaud Valmary
  • 2,039
  • 9
  • 14
  • Add `$5` as the pattern in `error.awk`, and you can get rid of `confirm error` as well: `trimline | awk -f error.awk` – chepner Sep 16 '21 at 18:52
  • @chepner: I have updated the awk code with trim and $5 filter – Arnaud Valmary Sep 16 '21 at 19:15
  • @ArnaudValmary Unexpected symbol `/^' near syntax error – YYZZYY Sep 16 '21 at 19:37
  • @YYZZYY: It's not a bash program. It's awk program. Use It with this command : `cat error.txt | awk -f error.awk`. or if you want to execute it like a shell script, add on first line: `#! /bin/awk -f` and the good rights to execute it! – Arnaud Valmary Sep 16 '21 at 19:48
  • @ArnaudValmary Can this be converted into a function? Used #!/bin/awk -f and still cannot be used – YYZZYY Sep 17 '21 at 04:30
  • Converted into a function ? You can integrate your awk program in a shell code. If `/bin/awk` does not exists `/usr/bin/awk` exists. Sometime, I found this shebang: `#! /usr/bin/env -S awk -f` – Arnaud Valmary Sep 17 '21 at 05:42
  • Don't use cat and a pipe (`cat error.txt | ..`) to open an input file, see https://porkmail.org/era/unix/award.html, and don't use a shebang to call awk (`#!/bin/awk...`), see https://stackoverflow.com/a/61002754/1745001. – Ed Morton Sep 17 '21 at 16:42
  • The `FS=":"` in the first block of code is too late as the input has already been split into fields before that block is interpretted, and the `FS=" "` at the start of the 2nd block won't for anything for the first line read but it will set FS back to it's default value for every subsequent line. It's not at all clear what the intent is of those settings but what they will do is - nothing at all, every time a line of input is read FS will be the default value of `" "` and though re-splitting will happen on each gsub() you don't use the new fields afterwards. – Ed Morton Sep 17 '21 at 19:10
0

Is this what you're trying to do?

$ 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] != "" {
        deviceName = $4
        processId = processName = $5
        gsub(/.*\[|].*/,"",processId)
        description = $0
        sub(/[^(]*/,"",description)

        print deviceName
        print processId
        print processName
        print description
    }
'

$ ./tst.sh
BBAOMACBOOKAIR2
1
com.apple.xpc.launchd[1]
(com.apple.mdworker.bundles[12513]): Could not find uid associated with service: 0: Undefined error: 0 501
BBAOMACBOOKAIR2
1
com.apple.xpc.launchd[1]
(com.apple.mdworker.bundles[12513]): Service exited with abnormal code: 78
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