将PID与GREP匹配的正则是吗?

发布于 2025-02-13 03:48:59 字数 326 浏览 2 评论 0原文

我想匹配6202/java6202

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赢得她心 2025-02-20 03:48:59

您可能对另一种不同的选择感兴趣。命令lsof允许您根据程序名称查询。

$ lsof -Pan -i -c java

这输出了一些表格数据。如果将-f String添加到其中,可以询问它以易于使用awk和sed等工具处理的方式输出。默认值将仅输出PID和文件描述符,因此您可以执行类似的操作:

$ lsof -Pan -i -F'' -c java | awk '/^p/{print substr($0,2)}'

lsof的详细信息可以在man lsof中找到。

You might be interested in a somewhat different alternative. The command lsof allows you to query based on program name.

$ lsof -Pan -i -c java

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:

$ lsof -Pan -i -F'' -c java | awk '/^p/{print substr($0,2)}'

Details of lsof can be found in man lsof

乖不如嘢 2025-02-20 03:48:59

您可以将GREP命令写为:

sudo netstat -tulpn | grep -o "[^[:space:]]\+java[[:space:]]*$"

使用-O

仅打印匹配行的匹配(非空)部分
此部分在单独的输出线上。

模式匹配:

  • [^[:SPACE:]] \+匹配1+非whitespace chars
  • java java 字面意思是
  • [[:space:]]*匹配可选空间
  • $字符串

输出

6202/java

的结尾,如果要拥有数字,并且-p为Perl兼容的正则支持您可以匹配1+数字和断言/java在字符串末尾的右侧:

sudo netstat -tulpn | grep -oP "\d+(?=/java\s*$)"

输出

6202

You can write the grep command as:

sudo netstat -tulpn | grep -o "[^[:space:]]\+java[[:space:]]*
quot;

Using -o

Print only the matched (non-empty) parts of a matching line, with each
such part on a separate output line.

The pattern matches:

  • [^[:space:]]\+ Match 1+ non whitespace chars
  • java Match literally
  • [[:space:]]* Match optional spaces
  • $ End of string

Output

6202/java

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:

sudo netstat -tulpn | grep -oP "\d+(?=/java\s*$)"

Output

6202
被你宠の有点坏 2025-02-20 03:48:59

使用您显示的样品和尝试,请尝试以下代码。

第1个解决方案: 运行netstat带有tulpn选项命令,并将其输出发送到awk代码。在中,awk代码将字段分隔符作为空格或/,然后检查最后一个字段是否为Java,然后打印该行的最后2个字段。

sudo netstat -tulpn | 
awk -F' |/' '
 $NF=="java"{
  print $(NF-1),$NF
 }
'


第二解决方案: 运行netstat带有tulpn选项,并将其输出发送到awk代码。在中,awk使用split split 用/拆分最后一个字段,并创建一个名为arr的数组,并检查进一步的条件,如果该数组的第二个元素是Java,则打印该数组或打印最后一个字段的两个元素(将是该行的最后一个和第二个字段)。

sudo netstat -tulpn | 
awk '
split($NF,arr,"/")==2 && arr[2]=="java"'{
  print $NF
}'


第三解决方案: 使用GNU GREP尝试以下代码。使用其 - op选项分别打印精确匹配的值并启用PCRE REGEX。在主要程序匹配值之前,直到最后一个空间发生,然后是\ k忘记匹配的正则和匹配的数字(1或更多出现),并确保其后面是/java使用积极的观察机制。

sudo netstat -tulpn | grep -oP '.*[[:space:]]+\K\d+(?=/java)'

With your shown samples and attempts, please try following codes.

1st solution: Running netstat command with tulpn options and sending its output to awk code. In awk code making field separators as either space OR / and then checking if last field is java then print last 2 fields of that line.

sudo netstat -tulpn | 
awk -F' |/' '
 $NF=="java"{
  print $(NF-1),$NF
 }
'


2nd solution: Running netstat command with tulpn options and sending its output to awk code. In awk code using split function to split last field with separator as / and creating an array named arr 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).

sudo netstat -tulpn | 
awk '
split($NF,arr,"/")==2 && arr[2]=="java"'{
  print $NF
}'


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.

sudo netstat -tulpn | grep -oP '.*[[:space:]]+\K\d+(?=/java)'
说谎友 2025-02-20 03:48:59
  awk nf ++ fs ='。*'ofs =

mawk'$!nf = $ nf' 
gawk'$ _ = $ nf'
 
6202/java

这就是最后一列所需的。仅在Java出现时保留:

  awk'index($!++ nf,“/java”)'fs ='。*'ofs ='
mawk'$!nf = $(nf *=/\/java $/)' 
 
6202/java

如果您不介意1个特大领先空间:

  mawk -f'。*''nf* =/\/java $/'
 
 6202/java

如果您 只是 想要pid

  mawk'nf- =/\/java $/'fs ='^。* |/'ofs =
 
6202
awk NF++ FS='.* ' OFS=

mawk '$!NF= $NF' 
gawk  '$_ = $NF'
6202/java

That's all u need for last column. To keep only when java is there :

 awk 'index($!++NF, "/java")' FS='.* ' OFS=
mawk '$!NF=$(NF *=/\/java$/)' 
6202/java

If you don't mind 1 extra leading space :

mawk -F'.* ' 'NF*=/\/java$/'
 6202/java

And if you just want the PID,

mawk 'NF -= /\/java$/' FS='^.* |/' OFS=
6202
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文