bash 如果 grep 值大于 1000 那么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25464618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
if grep value greater than 1000 then
提问by user3022917
I want to grep "priority=" in a file where the value of "priority=" is greater than 1000.
我想在“priority=”的值大于 1000 的文件中搜索“priority=”。
I tried something like this:
我试过这样的事情:
if grep -q "priority=[ >1000 ]" file; then
echo "[!] Unnatural priority"
fi
e.g. <intent-filter android:priority="2147483647">
例如 <intent-filter android:priority="2147483647">
回答by jm666
Try:
尝试:
(( $(grep -oP 'priority\s*=\s*"\s*\K(\d+)' file) > 1000 )) && echo "warning"
Need a relatively new grep with -P
perl regex support. The:
需要一个具有-P
perl 正则表达式支持的相对较新的 grep 。这:
\K
(variable look behind) matches, but kills everything before it from the result, so it prints only the capture group(\d+)
\K
(variable look behind) 匹配,但从结果中杀死它之前的所有内容,因此它仅打印捕获组(\d+)
of course, you can use perl too,
当然,你也可以使用 perl,
perl -nlE 'say if /priority="\K(\d+)/' <<< '<intent-filter android:priority="2147483647">'
prints
印刷
2147483647
or sed
或 sed
sed 's/.*priority="\([0-9][0-9]*\).*//' <<< '<intent-filter android:priority="2147483647">'
回答by Tom Fenech
You could use this Perl one-liner:
你可以使用这个 Perl one-liner:
perl -lne 'print "[!] Unnatural priority" if /priority="(\d+)"/ && > 1000'
Capture the digits in priority="X" and print the warning if the value is greater than 1000.
捕获 priority="X" 中的数字并在值大于 1000 时打印警告。
You can also do this in native bash if you want:
如果需要,您也可以在本机 bash 中执行此操作:
while read -r line; do
if [[ $line =~ priority=\"([[:digit:]]+)\" ]] && (( BASH_REMATCH[1] > 1000 )); then
echo "[!] Unnatural priority"
fi
done < file
回答by Bobulous
You could try using a regular expression to require a pattern that resembles a number greater than one thousand:
您可以尝试使用正则表达式来要求类似于大于一千的数字的模式:
grep -q --regexp="priority=\"[1-9][0-9]\{3,\}\"" file
This should match the case where priority=
is followed by at least four digits and the first digit is non-zero.
这应该与priority=
后面至少有四位数字且第一位非零的情况相匹配。
回答by Simon Woodside
awk
will make this easy:
awk
将使这变得容易:
$ cat file | awk -F '=' ' > 1000 {print ##代码##}'
Assuming that there's only one =
on each line of course.
当然,假设=
每一行只有一个。