问题
I have a program that when I run, it prints something like this in command line:
file test.test: 427 sentences, 2433 words, 1186 OOVs
0 zeroprobs, logprob= -4914.55 ppl= 862.603 ppl1= 8731.65
But I only want to save the number 862.603 in an environment variable. How can I extract that single number from output of the program?
回答1:
Every answer I've seen thus far has had some deficiency, so I guess I'll go ahead and add this to the mix:
There are a couple of ways to do this.
My preferred way of doing it would be with grep's perl extension (-P
):
var=$(myProgram | grep -oP 'ppl=\s*\K\d+\.\d+')
The -o
flag tells grep to only print the matching string, which in this case is the number you're looking for.
Note that this is (almost) identical to sputnick's solution, but I've adapted it to read from your program directly via a pipe rather than a from a file.
I prefer that solution because you're essentially wanting to search for a particular string, which grep excels at. The only problem is you also want to do a lookbehind, which is only supported in the perl regex extension.
So, if your grep doesn't support the perl regex extension, I would use sed:
var=$(myProgram | sed 's/ppl=\s*\(\d\+\.\d\+\)/\1/')
This assumes gnu sed, which is pretty common. If you don't have gnu sed, then use this:
var=$(myProgram | sed 's/ppl=[ \t]*\([0-9]\{1,\}\.[0-9]\{1,\})/\1/')
The bottom line here is, you absolutely do not need more than one pipe to accomplish this task. Opening pipes mean starting new processes, which is expensive. In general, you want to open up as few pipes as necessary to accomplish your task when coding in the shell.
Edit
Just to point it out: sputnick's answer is now pretty much exactly what you want if you have the perl extension available. The only difference between his and mine now is a small regex change (which you'll likely have to tweak yourself to suit your needs in the end anyways).
回答2:
$ var=$(<YOUR_COMMAND> | grep -oP "ppl= \K\d+\.\d+")
$ echo $var
862.603
If your distro lack the grep -P
option, you should install pcregrep
Edit: Post edited to better suit your needs : I was using grep on a file, now that's on your command.
Edit2: this is an awk
way to do it :
var=$(
<YOUR_COMMAND> |
awk '
/ppl=/{
for (i=0; i<NF; i++) {
if ($(i) ~ "ppl=" && $(i) > 1) {
print $(i+1)
}
}
}'
)
echo $var
回答3:
You could use fgrep
to get only the line you want, and awk
to get only the number you want:
program | fgrep ppl= | awk '{ print $6 }'
回答4:
$ MY_ENV_VAR="$(myprogram |grep 'ppl= '|sed -r 's:^.*ppl= ([0-9\.]+).*:\1:'
)"
回答5:
Try with this:
var=`./program | grep 'ppl=' | cut -d= -f3 | sed 's/ //g' | sed 's/ppl1//g'`
来源:https://stackoverflow.com/questions/10873456/how-to-extract-ppl-of-this-custom-program