将PID与GREP匹配的正则是吗?
我想匹配6202/java
或6202
tcp6 0 0 127.0.0.1:2014 :::* LISTEN 6202/java
与Regex Web工具,我可以匹配它,但是如果我使用“ \ s*$”正则是将非白空间匹配到字符串末端,我什么都没有得到。
sudo netstat -tulpn | grep java | grep -o "\S*$"
I want to match 6202/java
or 6202
tcp6 0 0 127.0.0.1:2014 :::* LISTEN 6202/java
With regex web tooling I am able to match it but if I use "\S*$"
regex to match non white space up to the end of the string I get nothing.
sudo netstat -tulpn | grep java | grep -o "\S*quot;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能对另一种不同的选择感兴趣。命令
lsof
允许您根据程序名称查询。这输出了一些表格数据。如果将
-f String
添加到其中,可以询问它以易于使用awk和sed等工具处理的方式输出。默认值将仅输出PID和文件描述符,因此您可以执行类似的操作:lsof
的详细信息可以在man lsof
中找到。You might be interested in a somewhat different alternative. The command
lsof
allows you to query based on program name.This outputs some tabulated data. If you add
-F string
to it, you can ask it what to output in a way that is easily processable with tools such as awk and sed. The default will just output the pid and file descriptors, so you can do something like:Details of
lsof
can be found inman lsof
您可以将GREP命令写为:
使用
-O
模式匹配:
[^[:SPACE:]] \+
匹配1+非whitespace charsjava
java 字面意思是[[:space:]]*匹配可选空间
$
字符串输出
的结尾,如果要拥有数字,并且
-p
为Perl兼容的正则支持您可以匹配1+数字和断言/java
在字符串末尾的右侧:输出
You can write the grep command as:
Using
-o
The pattern matches:
[^[:space:]]\+
Match 1+ non whitespace charsjava
Match literally[[:space:]]*
Match optional spaces$
End of stringOutput
If you want to have the digits only and
-P
is supported for a perl compatible regex you can match 1+ digits and assert/java
to the right at the end of the string:Output
使用您显示的样品和尝试,请尝试以下代码。
第1个解决方案: 运行
netstat
带有tulpn
选项命令,并将其输出发送到awk
代码。在中,awk
代码将字段分隔符作为空格或/
,然后检查最后一个字段是否为Java
,然后打印该行的最后2个字段。第二解决方案: 运行
netstat
带有tulpn
选项,并将其输出发送到awk
代码。在中,awk
使用split
split 用/
拆分最后一个字段,并创建一个名为arr
的数组,并检查进一步的条件,如果该数组的第二个元素是Java,则打印该数组或打印最后一个字段的两个元素(将是该行的最后一个和第二个字段)。第三解决方案: 使用GNU
GREP
尝试以下代码。使用其 -op
选项分别打印精确匹配的值并启用PCRE REGEX。在主要程序匹配值之前,直到最后一个空间发生,然后是\ k
忘记匹配的正则和匹配的数字(1或更多出现),并确保其后面是/java
使用积极的观察机制。With your shown samples and attempts, please try following codes.
1st solution: Running
netstat
command withtulpn
options and sending its output toawk
code. Inawk
code making field separators as either space OR/
and then checking if last field isjava
then print last 2 fields of that line.2nd solution: Running
netstat
command withtulpn
options and sending its output toawk
code. Inawk
code usingsplit
function to split last field with separator as/
and creating an array namedarr
AND checking further condition if 2nd element of that array is java then print both of the elements of that array OR print last field(which will be last and 2nd last field of that line).3rd solution: Using GNU
grep
try following code. Using its -oP
options to print exact matched value and enabling PCRE regex respectively. In main program matching value till last space occurs followed by a\K
to forget that matched regex and matching digits(1 or more occurrences) and making sure its followed by/java
using positive look ahead mechanism.这就是最后一列所需的。仅在Java出现时保留:
如果您不介意1个特大领先空间:
如果您 只是 想要
pid
,That's all u need for last column. To keep only when java is there :
If you don't mind 1 extra leading space :
And if you just want the
PID
,