简体   繁体   中英

interactive parsing 2 lines with awk or other utility maybe?

I have program running in background and when it's received certain message, it will give output 2 lines like this:

09/09 15:28:29 Tx PL HouseUnit: A2
09/09 15:28:29 Tx PL House: A Func: Off

Now I want to parse word A2 and Off to publish mqtt message by issuing command:

mosquitto_pub -q 1 -t X10/"A2" -m "Off"

I'm thinking to parse with awk but my problem is awk only parse 1 line interactively. Anybody has smart suggestion to resolve this issue?

awk '{if($3 == "Tx" && $4 == "PL" && $5 == "HouseUnit:") print $6; if($3 == "Tx" && $4 == "PL" && $5 == "House:" && $7 == "Func:") print $8} {system("mosquitto_pub -q 1 -t X10/127.0.0.1/switch/"$6 -m "$8) }'

code above only parse last line which execute

mosquitto_pub -q 1 -t X10/"A" -m "Off"

This is the syntax for what you're trying to do:

$ awk '
    ($3 == "Tx") && ($4 == "PL") {
        if ($5 == "HouseUnit:") {
            foo = $6
        }
        else if ( ($5 == "House:") && ($7 == "Func:") ) {
            system("echo mosquitto_pub -q 1 -t X10/127.0.0.1/switch/\"" foo "\" -m \"" $8 "\"")
        }
    }
' file
mosquitto_pub -q 1 -t X10/127.0.0.1/switch/"A2" -m "Off"

(remove the echo after testing) but idk if calling that program from system() really makes sense vs just outputting the values from awk and calling it from a pipe to xargs or similar. Rename the variable foo to be something representative of whatever it actually means.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM