tags:

views:

291

answers:

2

I've created an awk script and use 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 is searching for either "PID:" or "MPOS"?

awk-script is:

#!/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)
}
+1  A: 

If you just want to search for PID: or MPOS, you can say that if you don't find them in the line, you want to skip 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)
}
Johannes Schaub - litb
This worked in ubuntu using authentic 'awk', but in my embedded environment (where grep and awk are 'busybox' implementations), I still have grief. I tried even replacing the "/Sleeve/" match with "/PID:.*Sleeve/" without success. I'm stuck (no hardship) for now using my original command line.
Jamie
If `!/PID:|MPOS/` doesn't work, try `!(/PID:/ || /MPOS/) {next}`
glenn jackman
I should point out that the syntax 'litb' suggested is fine - there's something in my input file that is causing the script to behave in unpredictable ways. Simple test files show the script 'litb' suggested works (in busybox and otherwise).
Jamie
+1  A: 

I tried litb's answer in busybox (on Ubuntu in Bash) and it worked for me. For testing, I used the following shebang to match where I have symbolic links to busybox:

#!/home/username/busybox/awk -f

And ran the test using:

./awk-script file.txt

I also ran the test under busybox sh (with PATH=/home/username/busybox:$PATH although not necessary for this) and it worked there.

When you say "I still have grief." what does that mean? Are you getting error messages or incorrect results?

By the way, unless you're searching for all permutations of the characters, you can do your grep like this:

grep -E "(PID:|MPOS)" file.txt
Dennis Williamson
The syntax is fine, it just truncates the output to a few dozen lines instead the 10K plus that I'm expecting (when running in the embedded system). The script and test files are the same and the command line was cut and paste between the two environments.So by 'grief' I mean I get pleasing lengthy results in Ubuntu, and pleasing truncated results in the embedded. I'm fairly certain there's something in the file that is the cause that might have gotten introduced in the transfer between systems. Not important, the grep works well. (+1 for the grep tip).
Jamie