sed removing everything until and including the first period if there's more than one period on that line and do this for the whole file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












sed removing everything until and including the first period if there is more than one period on that line and do this for the whole file.



Before sed:



akamai.com
cdnjs.cloudflare.com
com.cdn.cloudflare.net


After sed:



akamai.com
cloudflare.com
cdn.cloudflare.net






share|improve this question



























    up vote
    4
    down vote

    favorite












    sed removing everything until and including the first period if there is more than one period on that line and do this for the whole file.



    Before sed:



    akamai.com
    cdnjs.cloudflare.com
    com.cdn.cloudflare.net


    After sed:



    akamai.com
    cloudflare.com
    cdn.cloudflare.net






    share|improve this question























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      sed removing everything until and including the first period if there is more than one period on that line and do this for the whole file.



      Before sed:



      akamai.com
      cdnjs.cloudflare.com
      com.cdn.cloudflare.net


      After sed:



      akamai.com
      cloudflare.com
      cdn.cloudflare.net






      share|improve this question













      sed removing everything until and including the first period if there is more than one period on that line and do this for the whole file.



      Before sed:



      akamai.com
      cdnjs.cloudflare.com
      com.cdn.cloudflare.net


      After sed:



      akamai.com
      cloudflare.com
      cdn.cloudflare.net








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 27 at 17:30









      slm♦

      232k65479649




      232k65479649









      asked Jul 27 at 17:09









      Bjorn

      302




      302




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          12
          down vote



          accepted










          $ sed '/..*./s/^[^.]*.//' file
          akamai.com
          cloudflare.com
          cdn.cloudflare.net


          The sed script first matches lines that contain at least two dots using the regular expression ..*. (could also have been written [.].*[.]). For lines matching this, a substitution that removes everything up to and including the first dot is performed.



          Using awk, being somewhat long-winded in comparison to the above:



          $ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
          akamai.com
          cloudflare.com
          cdn.cloudflare.net


          Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1 at the end causes every line (modified or not) to be printed.



          Shorter awk in the same fashion as the sed solution:



          $ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
          akamai.com
          cloudflare.com
          cdn.cloudflare.net





          share|improve this answer























          • It is possible to use only one regex in sed: sed '/.([^.]*.)/s//1/' infile and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
            – Isaac
            Jul 27 at 21:03










          • Grep gets a bit complex: grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
            – Isaac
            Jul 27 at 21:21

















          up vote
          1
          down vote













          You could approach this using the following methods:



          perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt


          wherein, we rely on a regex that zeroes in on a dot . character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1 and stuffed inside the current line. The -p option to Perl then takes this to the stdout as well as the one that didn't match as well.




          perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt


          • Input file is read line-by-line via the -p option and autoprint is also turned on via this option as well.


          • Each record is split on the dot . and the individual fields stored in the array @F indexed starting from 0 via the -a option.


          • -l option makes RS = ORS = "n"


          • Only when we have more than 2 elements in the array @F, meaning there were atleast 2 dots in the current record, we select such a record for modifications.


          • For such a record, the splice @F, 1 function strips away elements 2nd onwards and presents them to the join function which then joins them using the dot character, this then is stuffed inside the $_ , aka, current record.


          • The -p option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.



          Using GNU sed we can also do the task without taking recourse of capturing parens:



          sed -e 
          s/./n/2;T
          y/n./.n/
          s/n/./2g
          s/.*n//
          ' input.file



          Output:



          akamai.com
          cloudflare.com
          cdn.cloudflare.net





          share|improve this answer





















            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%2f458906%2fsed-removing-everything-until-and-including-the-first-period-if-theres-more-tha%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            12
            down vote



            accepted










            $ sed '/..*./s/^[^.]*.//' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net


            The sed script first matches lines that contain at least two dots using the regular expression ..*. (could also have been written [.].*[.]). For lines matching this, a substitution that removes everything up to and including the first dot is performed.



            Using awk, being somewhat long-winded in comparison to the above:



            $ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net


            Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1 at the end causes every line (modified or not) to be printed.



            Shorter awk in the same fashion as the sed solution:



            $ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net





            share|improve this answer























            • It is possible to use only one regex in sed: sed '/.([^.]*.)/s//1/' infile and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
              – Isaac
              Jul 27 at 21:03










            • Grep gets a bit complex: grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
              – Isaac
              Jul 27 at 21:21














            up vote
            12
            down vote



            accepted










            $ sed '/..*./s/^[^.]*.//' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net


            The sed script first matches lines that contain at least two dots using the regular expression ..*. (could also have been written [.].*[.]). For lines matching this, a substitution that removes everything up to and including the first dot is performed.



            Using awk, being somewhat long-winded in comparison to the above:



            $ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net


            Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1 at the end causes every line (modified or not) to be printed.



            Shorter awk in the same fashion as the sed solution:



            $ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net





            share|improve this answer























            • It is possible to use only one regex in sed: sed '/.([^.]*.)/s//1/' infile and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
              – Isaac
              Jul 27 at 21:03










            • Grep gets a bit complex: grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
              – Isaac
              Jul 27 at 21:21












            up vote
            12
            down vote



            accepted







            up vote
            12
            down vote



            accepted






            $ sed '/..*./s/^[^.]*.//' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net


            The sed script first matches lines that contain at least two dots using the regular expression ..*. (could also have been written [.].*[.]). For lines matching this, a substitution that removes everything up to and including the first dot is performed.



            Using awk, being somewhat long-winded in comparison to the above:



            $ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net


            Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1 at the end causes every line (modified or not) to be printed.



            Shorter awk in the same fashion as the sed solution:



            $ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net





            share|improve this answer















            $ sed '/..*./s/^[^.]*.//' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net


            The sed script first matches lines that contain at least two dots using the regular expression ..*. (could also have been written [.].*[.]). For lines matching this, a substitution that removes everything up to and including the first dot is performed.



            Using awk, being somewhat long-winded in comparison to the above:



            $ awk -F '.' -vOFS='.' 'NF > 2 n=split($0, a); $0=""; for (i=2;i<=n;++i) $(NF+1)=a[i] 1' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net


            Here, whenever there are more than two dot-delimited fields, we split the current line on dots, and then re-create the current record from that, skipping the first field. The trailing 1 at the end causes every line (modified or not) to be printed.



            Shorter awk in the same fashion as the sed solution:



            $ awk -F '.' 'NF > 2 sub("^[^.]*.", "") 1' file
            akamai.com
            cloudflare.com
            cdn.cloudflare.net






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jul 27 at 20:59


























            answered Jul 27 at 17:17









            Kusalananda

            101k13199311




            101k13199311











            • It is possible to use only one regex in sed: sed '/.([^.]*.)/s//1/' infile and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
              – Isaac
              Jul 27 at 21:03










            • Grep gets a bit complex: grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
              – Isaac
              Jul 27 at 21:21
















            • It is possible to use only one regex in sed: sed '/.([^.]*.)/s//1/' infile and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
              – Isaac
              Jul 27 at 21:03










            • Grep gets a bit complex: grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
              – Isaac
              Jul 27 at 21:21















            It is possible to use only one regex in sed: sed '/.([^.]*.)/s//1/' infile and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
            – Isaac
            Jul 27 at 21:03




            It is possible to use only one regex in sed: sed '/.([^.]*.)/s//1/' infile and the awk could be a lot smaller: awk '$0~resub(/^[^.]*./,"")1' re='^[^.]*\.([^.]*\.)' infile
            – Isaac
            Jul 27 at 21:03












            Grep gets a bit complex: grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
            – Isaac
            Jul 27 at 21:21




            Grep gets a bit complex: grep -P '^(?=[^.]*.[^.]*.)[^.]*.K.*|.*' infile
            – Isaac
            Jul 27 at 21:21












            up vote
            1
            down vote













            You could approach this using the following methods:



            perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt


            wherein, we rely on a regex that zeroes in on a dot . character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1 and stuffed inside the current line. The -p option to Perl then takes this to the stdout as well as the one that didn't match as well.




            perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt


            • Input file is read line-by-line via the -p option and autoprint is also turned on via this option as well.


            • Each record is split on the dot . and the individual fields stored in the array @F indexed starting from 0 via the -a option.


            • -l option makes RS = ORS = "n"


            • Only when we have more than 2 elements in the array @F, meaning there were atleast 2 dots in the current record, we select such a record for modifications.


            • For such a record, the splice @F, 1 function strips away elements 2nd onwards and presents them to the join function which then joins them using the dot character, this then is stuffed inside the $_ , aka, current record.


            • The -p option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.



            Using GNU sed we can also do the task without taking recourse of capturing parens:



            sed -e 
            s/./n/2;T
            y/n./.n/
            s/n/./2g
            s/.*n//
            ' input.file



            Output:



            akamai.com
            cloudflare.com
            cdn.cloudflare.net





            share|improve this answer

























              up vote
              1
              down vote













              You could approach this using the following methods:



              perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt


              wherein, we rely on a regex that zeroes in on a dot . character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1 and stuffed inside the current line. The -p option to Perl then takes this to the stdout as well as the one that didn't match as well.




              perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt


              • Input file is read line-by-line via the -p option and autoprint is also turned on via this option as well.


              • Each record is split on the dot . and the individual fields stored in the array @F indexed starting from 0 via the -a option.


              • -l option makes RS = ORS = "n"


              • Only when we have more than 2 elements in the array @F, meaning there were atleast 2 dots in the current record, we select such a record for modifications.


              • For such a record, the splice @F, 1 function strips away elements 2nd onwards and presents them to the join function which then joins them using the dot character, this then is stuffed inside the $_ , aka, current record.


              • The -p option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.



              Using GNU sed we can also do the task without taking recourse of capturing parens:



              sed -e 
              s/./n/2;T
              y/n./.n/
              s/n/./2g
              s/.*n//
              ' input.file



              Output:



              akamai.com
              cloudflare.com
              cdn.cloudflare.net





              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                You could approach this using the following methods:



                perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt


                wherein, we rely on a regex that zeroes in on a dot . character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1 and stuffed inside the current line. The -p option to Perl then takes this to the stdout as well as the one that didn't match as well.




                perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt


                • Input file is read line-by-line via the -p option and autoprint is also turned on via this option as well.


                • Each record is split on the dot . and the individual fields stored in the array @F indexed starting from 0 via the -a option.


                • -l option makes RS = ORS = "n"


                • Only when we have more than 2 elements in the array @F, meaning there were atleast 2 dots in the current record, we select such a record for modifications.


                • For such a record, the splice @F, 1 function strips away elements 2nd onwards and presents them to the join function which then joins them using the dot character, this then is stuffed inside the $_ , aka, current record.


                • The -p option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.



                Using GNU sed we can also do the task without taking recourse of capturing parens:



                sed -e 
                s/./n/2;T
                y/n./.n/
                s/n/./2g
                s/.*n//
                ' input.file



                Output:



                akamai.com
                cloudflare.com
                cdn.cloudflare.net





                share|improve this answer













                You could approach this using the following methods:



                perl -lpe '$_ = $1 if /.(.*..*)/' input-file.txt


                wherein, we rely on a regex that zeroes in on a dot . character that can see another dot to it's right. Then whatever is to the right is captured and made available in $1 and stuffed inside the current line. The -p option to Perl then takes this to the stdout as well as the one that didn't match as well.




                perl -F\. -pale '$_ = join ".", splice @F, 1 if @F > 2' input.txt


                • Input file is read line-by-line via the -p option and autoprint is also turned on via this option as well.


                • Each record is split on the dot . and the individual fields stored in the array @F indexed starting from 0 via the -a option.


                • -l option makes RS = ORS = "n"


                • Only when we have more than 2 elements in the array @F, meaning there were atleast 2 dots in the current record, we select such a record for modifications.


                • For such a record, the splice @F, 1 function strips away elements 2nd onwards and presents them to the join function which then joins them using the dot character, this then is stuffed inside the $_ , aka, current record.


                • The -p option then takes this modified current record to the stdout. The one that didnot modify is anyway taken silently to the stdout.



                Using GNU sed we can also do the task without taking recourse of capturing parens:



                sed -e 
                s/./n/2;T
                y/n./.n/
                s/n/./2g
                s/.*n//
                ' input.file



                Output:



                akamai.com
                cloudflare.com
                cdn.cloudflare.net






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jul 28 at 19:30









                Rakesh Sharma

                3333




                3333






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f458906%2fsed-removing-everything-until-and-including-the-first-period-if-theres-more-tha%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay