Reputation: 315
I run:
ss -atp | grep -vi state | awk '{ print $2" "$3" "$4" "$5" "$6 }'
output:
0 0 192.168.1.14:49254 92.222.106.156:http users:(("firefox-esr",pid=696,fd=95))
From the last column, I want to strip everything but firefox-esr
(in this case); more precisely I want to only fetch what's between ""
.
I have tried:
ss -atp | grep -vi state | awk '{ sub(/users\:\(\("/,"",$6); print $2" "$3" "$4" "$5" "$6 }'
0 0 192.168.1.14:49254 92.222.106.156:http firefox-esr",pid=696,fd=95))
There is still the last part to strip; the problem is that the pid
and fd
are not a constant value and keep changing.
Upvotes: 0
Views: 453
Reputation: 36370
You might harness gensub
reference ability for that. For simplicity let file.txt
content be
users:(("firefox-esr",pid=696,fd=95))
then
awk '{print gensub(/.*"(.+)".*/,"\\1",1,$1)}' file.txt
outputs:
firefox-esr
Keep in mind that gensub
do not alter string it gets as 4th argument, but return new string, so I print
it.
Upvotes: 1
Reputation: 626728
You can use
awk '{ gsub(/^[^\"]*\"|\".*/, "", $6); print $2" "$3" "$4" "$5" "$6 }'
Here, gsub(/^[^\"]*\"|\".*/, "", $6)
will take Field 6 as input, and remove all chars from start till the first "
including it (see the ^[^\"]*\"
part) and then the next "
and all text after it (using \".*
).
See this online awk
demo:
s='0 0 0 192.168.1.14:49254 92.222.106.156:http users:(("firefox-esr",pid=696,fd=95))'
awk '{gsub(/^[^\"]*\"|\".*/, "",$6); print $2" "$3" "$4" "$5" "$6 }' <<< "$s"
# => 0 0 192.168.1.14:49254 92.222.106.156:http firefox-esr
Upvotes: 1