[英]Finding particular character in a line in bash
Is there a way to extract a particular part from a line of text with regular expression in bash. 有没有办法在bash中使用正则表达式从一行文本中提取特定部分。
For example 例如
8495 pts/1 S 0:10 /opt/sbin/proc -q 10 -p /opt/etc/plugin/ -c /opt/etc/ -f 200000 -s 10000 -b 3276
How can I extract q parameter 10 from above?. 如何从上面提取q参数10 ? I need the number after q parameter
我需要q参数后面的数字
px aux | sed -n 's/.*-q\s\([0-9]\+\).*/\1/p'
The naive solution regex would be .*-q ([^ ]*) .*
. 天真的解决方案正则表达式将是
.*-q ([^ ]*) .*
。 So, something like sed 's/.*-q ([^ ]*) .*/\\1/'
should do the trick. 所以,像
sed 's/.*-q ([^ ]*) .*/\\1/'
应该可以解决问题。
If the line is stored in variable $line
: 如果该行存储在变量
$line
:
val=$(
this_token=false
for word in $line; do
$this_token && { echo $word; break; }
[[ $word = "-q" ]] && this_token=true
done
)
echo $val
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.