Home > Blockchain >  How to pass parameters and output in the required format
How to pass parameters and output in the required format

Time:09-21

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

CodePudding user response:

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

CodePudding user response:

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
  • Related