Follow

how to select
`pid number`
in
`users:(("postgres",pid=1644,fd=3))`
and pipe then to next command in terminal?

@Acer

echo 'users:(("postgres",pid=1644,fd=3))' | sed -E 's/^.*pid=([0-9]+).*$/\1/'

@Acer
echo -e "users:(("postgres",pid=1644,fd=3))" | grep -oP '(?<=pid=).*(?=,)'

@hakerdefo

Thanks I ll try to learn the options and the expression

@hakerdefo

-o
only

-P
perl-regexp

grep -options PATTERNS

PATTERNS

'......'
I didn't find definitions about this.

(...)
Grouping with Parentheses

(?=pattern)
is a positive look-ahead assertion

(?<=pattern)
is a positive look-behind assertion

I have one I cannot understand
what's the difference between
PATTERNS
and
'PATTERNS'

@Acer
Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \) with a backslash (\).

@Acer

Assume the string is in parameter x then...

y=${x##*pid=}; echo ${y%%,*}

This is a way to do it without launching any additional processes.

(Note: There may be a way to nest these parameter expansions into one expression, but I don't know how to do that, if it's possible.)

@Acer

When I tooted the first toot, it thought that the stars were to highlight text for italics, that's why they didn't print in the first toot. So, I made it an image instead.

This is "parameter expansion" in BASH.

man bash

section "Parameter Expansion"

@Acer

here it is in plain text...

y=${x##*pid=}; echo ${y%%,*}

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.