How to filter several strings

The name of the pictureThe name of the pictureThe name of the pictureClash 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









share|improve this question



























    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









    share|improve this question

























      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









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 hours ago









      Jeff Schaller

      33.6k851113




      33.6k851113










      asked 2 hours ago









      David

      129110




      129110




















          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





          share|improve this answer




















          • Thank you so much.
            – David
            2 hours ago

















          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





          share|improve this answer










          New contributor




          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















          • Thank you so much.
            – David
            1 hour ago






          • 1




            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

















          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





          share|improve this answer


















          • 1




            Thank you so much.
            – David
            2 hours ago

















          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





          share|improve this answer
















          • 1




            Thank you so much.
            – David
            1 hour ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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





          share|improve this answer




















          • Thank you so much.
            – David
            2 hours ago














          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





          share|improve this answer




















          • Thank you so much.
            – David
            2 hours ago












          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          Joseph Sible

          999213




          999213











          • Thank you so much.
            – David
            2 hours ago
















          • Thank you so much.
            – David
            2 hours ago















          Thank you so much.
          – David
          2 hours ago




          Thank you so much.
          – David
          2 hours ago












          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





          share|improve this answer










          New contributor




          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















          • Thank you so much.
            – David
            1 hour ago






          • 1




            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














          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





          share|improve this answer










          New contributor




          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.

















          • Thank you so much.
            – David
            1 hour ago






          • 1




            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












          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





          share|improve this answer










          New contributor




          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          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






          share|improve this answer










          New contributor




          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer








          edited 4 mins ago





















          New contributor




          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 2 hours ago









          Craig Comstock

          312




          312




          New contributor




          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Craig Comstock is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.











          • Thank you so much.
            – David
            1 hour ago






          • 1




            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
















          • Thank you so much.
            – David
            1 hour ago






          • 1




            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















          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










          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





          share|improve this answer


















          • 1




            Thank you so much.
            – David
            2 hours ago














          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





          share|improve this answer


















          • 1




            Thank you so much.
            – David
            2 hours ago












          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





          share|improve this answer














          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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 hours ago

























          answered 2 hours ago









          Goro

          6,62552865




          6,62552865







          • 1




            Thank you so much.
            – David
            2 hours ago












          • 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










          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





          share|improve this answer
















          • 1




            Thank you so much.
            – David
            1 hour ago














          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





          share|improve this answer
















          • 1




            Thank you so much.
            – David
            1 hour ago












          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





          share|improve this answer












          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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          DopeGhoti

          41.5k55180




          41.5k55180







          • 1




            Thank you so much.
            – David
            1 hour ago












          • 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

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery