How to filter several strings
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I need to filter from these lines only protocol,port and service.
I have these lines
tcp 127.0.0.1:25 1147/master
tcp 0.0.0.0:443 1039/nginx:
tcp 127.0.0.1:8001 1218/python
tcp 0.0.0.0:10050 939/zabbix_agentd
tcp 127.0.0.1:6379 891/redis-server
tcp 0.0.0.0:80 1039/nginx:
tcp 0.0.0.0:22 889/sshd
tcp 127.0.0.1:5432 929/postmaster
udp 127.0.0.1:323 645/chronyd
I need like this
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
linux text-processing awk sed grep
add a comment |Â
up vote
2
down vote
favorite
I need to filter from these lines only protocol,port and service.
I have these lines
tcp 127.0.0.1:25 1147/master
tcp 0.0.0.0:443 1039/nginx:
tcp 127.0.0.1:8001 1218/python
tcp 0.0.0.0:10050 939/zabbix_agentd
tcp 127.0.0.1:6379 891/redis-server
tcp 0.0.0.0:80 1039/nginx:
tcp 0.0.0.0:22 889/sshd
tcp 127.0.0.1:5432 929/postmaster
udp 127.0.0.1:323 645/chronyd
I need like this
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
linux text-processing awk sed grep
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I need to filter from these lines only protocol,port and service.
I have these lines
tcp 127.0.0.1:25 1147/master
tcp 0.0.0.0:443 1039/nginx:
tcp 127.0.0.1:8001 1218/python
tcp 0.0.0.0:10050 939/zabbix_agentd
tcp 127.0.0.1:6379 891/redis-server
tcp 0.0.0.0:80 1039/nginx:
tcp 0.0.0.0:22 889/sshd
tcp 127.0.0.1:5432 929/postmaster
udp 127.0.0.1:323 645/chronyd
I need like this
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
linux text-processing awk sed grep
I need to filter from these lines only protocol,port and service.
I have these lines
tcp 127.0.0.1:25 1147/master
tcp 0.0.0.0:443 1039/nginx:
tcp 127.0.0.1:8001 1218/python
tcp 0.0.0.0:10050 939/zabbix_agentd
tcp 127.0.0.1:6379 891/redis-server
tcp 0.0.0.0:80 1039/nginx:
tcp 0.0.0.0:22 889/sshd
tcp 127.0.0.1:5432 929/postmaster
udp 127.0.0.1:323 645/chronyd
I need like this
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
linux text-processing awk sed grep
linux text-processing awk sed grep
edited 2 hours ago
Jeff Schaller
33.6k851113
33.6k851113
asked 2 hours ago
David
129110
129110
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
3
down vote
accepted
Here's one way to do it:
sed -re 's/[^ ]+://' -e 's# +[0-9]+/# #' -e 's/:$//'
Explanation:
sed -r
- use sed in extended-regexp mode-e 's/[^ ]+://'
- remove everything before the first colon backwards until a space-e 's# +[0-9]+/# #'
- replace any number of spaces, a number, and a slash with a single space-e 's/:$//'
- remove a colon from the end of the line
Thank you so much.
â David
2 hours ago
add a comment |Â
up vote
3
down vote
This seems more straight forward and easy to remember. -F option to awk allows you to provide a regex class that includes the characters you need to split on.
awk -F '[ :/]' 'print $1,$3,$5' file
steve (below) mentioned a shorter way than I originally thought of:
- instead of cat file | ... just put file at the end
- commas in between dollar items adds spaces
New contributor
Thank you so much.
â David
1 hour ago
1
Could shorten that down further to justawk -F'[ :/]' 'print$1,$3,$5' file
â steve
33 mins ago
thanks @steve I like even shorter :) edited my answer
â Craig Comstock
3 mins ago
add a comment |Â
up vote
2
down vote
cat file | sed 's/[:/]/t/g' | awk 'print $1 " " $3 " " $5'
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chr
sed 's/[:/]/t/g' convert `:` and '/` to tab
1
Thank you so much.
â David
2 hours ago
add a comment |Â
up vote
2
down vote
With a single awk
:
$ awk ' print $1, gensub( /^.*:/, "", "1", $2 ), gensub( /[0-9]+//, "", "1", $3 ) ' input
tcp 25 master
tcp 443 nginx:
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx:
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
1
Thank you so much.
â David
1 hour ago
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Here's one way to do it:
sed -re 's/[^ ]+://' -e 's# +[0-9]+/# #' -e 's/:$//'
Explanation:
sed -r
- use sed in extended-regexp mode-e 's/[^ ]+://'
- remove everything before the first colon backwards until a space-e 's# +[0-9]+/# #'
- replace any number of spaces, a number, and a slash with a single space-e 's/:$//'
- remove a colon from the end of the line
Thank you so much.
â David
2 hours ago
add a comment |Â
up vote
3
down vote
accepted
Here's one way to do it:
sed -re 's/[^ ]+://' -e 's# +[0-9]+/# #' -e 's/:$//'
Explanation:
sed -r
- use sed in extended-regexp mode-e 's/[^ ]+://'
- remove everything before the first colon backwards until a space-e 's# +[0-9]+/# #'
- replace any number of spaces, a number, and a slash with a single space-e 's/:$//'
- remove a colon from the end of the line
Thank you so much.
â David
2 hours ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Here's one way to do it:
sed -re 's/[^ ]+://' -e 's# +[0-9]+/# #' -e 's/:$//'
Explanation:
sed -r
- use sed in extended-regexp mode-e 's/[^ ]+://'
- remove everything before the first colon backwards until a space-e 's# +[0-9]+/# #'
- replace any number of spaces, a number, and a slash with a single space-e 's/:$//'
- remove a colon from the end of the line
Here's one way to do it:
sed -re 's/[^ ]+://' -e 's# +[0-9]+/# #' -e 's/:$//'
Explanation:
sed -r
- use sed in extended-regexp mode-e 's/[^ ]+://'
- remove everything before the first colon backwards until a space-e 's# +[0-9]+/# #'
- replace any number of spaces, a number, and a slash with a single space-e 's/:$//'
- remove a colon from the end of the line
answered 2 hours ago
Joseph Sible
999213
999213
Thank you so much.
â David
2 hours ago
add a comment |Â
Thank you so much.
â David
2 hours ago
Thank you so much.
â David
2 hours ago
Thank you so much.
â David
2 hours ago
add a comment |Â
up vote
3
down vote
This seems more straight forward and easy to remember. -F option to awk allows you to provide a regex class that includes the characters you need to split on.
awk -F '[ :/]' 'print $1,$3,$5' file
steve (below) mentioned a shorter way than I originally thought of:
- instead of cat file | ... just put file at the end
- commas in between dollar items adds spaces
New contributor
Thank you so much.
â David
1 hour ago
1
Could shorten that down further to justawk -F'[ :/]' 'print$1,$3,$5' file
â steve
33 mins ago
thanks @steve I like even shorter :) edited my answer
â Craig Comstock
3 mins ago
add a comment |Â
up vote
3
down vote
This seems more straight forward and easy to remember. -F option to awk allows you to provide a regex class that includes the characters you need to split on.
awk -F '[ :/]' 'print $1,$3,$5' file
steve (below) mentioned a shorter way than I originally thought of:
- instead of cat file | ... just put file at the end
- commas in between dollar items adds spaces
New contributor
Thank you so much.
â David
1 hour ago
1
Could shorten that down further to justawk -F'[ :/]' 'print$1,$3,$5' file
â steve
33 mins ago
thanks @steve I like even shorter :) edited my answer
â Craig Comstock
3 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
This seems more straight forward and easy to remember. -F option to awk allows you to provide a regex class that includes the characters you need to split on.
awk -F '[ :/]' 'print $1,$3,$5' file
steve (below) mentioned a shorter way than I originally thought of:
- instead of cat file | ... just put file at the end
- commas in between dollar items adds spaces
New contributor
This seems more straight forward and easy to remember. -F option to awk allows you to provide a regex class that includes the characters you need to split on.
awk -F '[ :/]' 'print $1,$3,$5' file
steve (below) mentioned a shorter way than I originally thought of:
- instead of cat file | ... just put file at the end
- commas in between dollar items adds spaces
New contributor
edited 4 mins ago
New contributor
answered 2 hours ago
Craig Comstock
312
312
New contributor
New contributor
Thank you so much.
â David
1 hour ago
1
Could shorten that down further to justawk -F'[ :/]' 'print$1,$3,$5' file
â steve
33 mins ago
thanks @steve I like even shorter :) edited my answer
â Craig Comstock
3 mins ago
add a comment |Â
Thank you so much.
â David
1 hour ago
1
Could shorten that down further to justawk -F'[ :/]' 'print$1,$3,$5' file
â steve
33 mins ago
thanks @steve I like even shorter :) edited my answer
â Craig Comstock
3 mins ago
Thank you so much.
â David
1 hour ago
Thank you so much.
â David
1 hour ago
1
1
Could shorten that down further to just
awk -F'[ :/]' 'print$1,$3,$5' file
â steve
33 mins ago
Could shorten that down further to just
awk -F'[ :/]' 'print$1,$3,$5' file
â steve
33 mins ago
thanks @steve I like even shorter :) edited my answer
â Craig Comstock
3 mins ago
thanks @steve I like even shorter :) edited my answer
â Craig Comstock
3 mins ago
add a comment |Â
up vote
2
down vote
cat file | sed 's/[:/]/t/g' | awk 'print $1 " " $3 " " $5'
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chr
sed 's/[:/]/t/g' convert `:` and '/` to tab
1
Thank you so much.
â David
2 hours ago
add a comment |Â
up vote
2
down vote
cat file | sed 's/[:/]/t/g' | awk 'print $1 " " $3 " " $5'
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chr
sed 's/[:/]/t/g' convert `:` and '/` to tab
1
Thank you so much.
â David
2 hours ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
cat file | sed 's/[:/]/t/g' | awk 'print $1 " " $3 " " $5'
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chr
sed 's/[:/]/t/g' convert `:` and '/` to tab
cat file | sed 's/[:/]/t/g' | awk 'print $1 " " $3 " " $5'
tcp 25 master
tcp 443 nginx
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx
tcp 22 sshd
tcp 5432 postmaster
udp 323 chr
sed 's/[:/]/t/g' convert `:` and '/` to tab
edited 2 hours ago
answered 2 hours ago
Goro
6,62552865
6,62552865
1
Thank you so much.
â David
2 hours ago
add a comment |Â
1
Thank you so much.
â David
2 hours ago
1
1
Thank you so much.
â David
2 hours ago
Thank you so much.
â David
2 hours ago
add a comment |Â
up vote
2
down vote
With a single awk
:
$ awk ' print $1, gensub( /^.*:/, "", "1", $2 ), gensub( /[0-9]+//, "", "1", $3 ) ' input
tcp 25 master
tcp 443 nginx:
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx:
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
1
Thank you so much.
â David
1 hour ago
add a comment |Â
up vote
2
down vote
With a single awk
:
$ awk ' print $1, gensub( /^.*:/, "", "1", $2 ), gensub( /[0-9]+//, "", "1", $3 ) ' input
tcp 25 master
tcp 443 nginx:
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx:
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
1
Thank you so much.
â David
1 hour ago
add a comment |Â
up vote
2
down vote
up vote
2
down vote
With a single awk
:
$ awk ' print $1, gensub( /^.*:/, "", "1", $2 ), gensub( /[0-9]+//, "", "1", $3 ) ' input
tcp 25 master
tcp 443 nginx:
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx:
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
With a single awk
:
$ awk ' print $1, gensub( /^.*:/, "", "1", $2 ), gensub( /[0-9]+//, "", "1", $3 ) ' input
tcp 25 master
tcp 443 nginx:
tcp 8001 python
tcp 10050 zabbix_agentd
tcp 6379 redis-server
tcp 80 nginx:
tcp 22 sshd
tcp 5432 postmaster
udp 323 chronyd
answered 1 hour ago
DopeGhoti
41.5k55180
41.5k55180
1
Thank you so much.
â David
1 hour ago
add a comment |Â
1
Thank you so much.
â David
1 hour ago
1
1
Thank you so much.
â David
1 hour ago
Thank you so much.
â David
1 hour ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473538%2fhow-to-filter-several-strings%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password