How can I fold this bash command line into an awk statement?
I created an awk script and am using it like this:
# grep -E "[PM][IP][DO][:S]" file.txt | awk-script
How can I modify the awk script to include the effort of the grep command (which looks for either "PID:" or "MPOS"?
awk-script:
#!/usr/bin/awk -f
/Sleeve/ {
printf("%8d, %7d, %7.2f, %7.2f, %7.2f\n", $5, $6, $7, $30, $31)
}
/Ambient/ {
printf("%8d, %7d,,,, %7.2f, %7.2f\n", $5, $6, $7, $8)
}
/MPOS:/ {
printf("%8d, %7d,,,,,, %5d, %5d\n", $4, $5, $2, $3)
}
source to share
If you just want to find PID:
or MPOS
, you can say that if you don't find them in the string, you should skip the following rules:
#!/usr/bin/awk -f
!/PID:|MPOS/ {
next
}
/Sleeve/ {
printf("%8d, %7d, %7.2f, %7.2f, %7.2f\n", $5, $6, $7, $30, $31)
}
/Ambient/ {
printf("%8d, %7d,,,, %7.2f, %7.2f\n", $5, $6, $7, $8)
}
/MPOS:/ {
printf("%8d, %7d,,,,,, %5d, %5d\n", $4, $5, $2, $3)
}
source to share
I tried litb's answer in busybox
(on Ubuntu in Bash) and it worked for me. For testing, I used the following shebang for comparison, where I have symlinks to busybox
:
#!/home/username/busybox/awk -f
And ran the test using:
./awk-script file.txt
I also ran a test under busybox sh
(with PATH=/home/username/busybox:$PATH
, although it wasn't necessary for that) and it worked there.
When you say, "I still have grief." what does it mean? Are you getting error messages or incorrect results?
By the way, if you are not looking for all character permutations, you can make your grep like this:
grep -E "(PID:|MPOS)" file.txt
source to share