Extract addresses separated by semicolon and print each address in a line

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











up vote
3
down vote

favorite
1












I have a file with the following input. The numbers separated by dots represents addresses. Any number in address can be one or more digits as follows:



[112.112.112.112;3.3.3.3;44.44.44.44]
[6.6.6.6;17.17.17.17;88.88.88.88]


I want to extract each address without the semicolons and brackets (addresses are separated by semicolon ;), and insert each address in a line in a new file to produce this output:



112.112.112.112
3.3.3.3
44.44.44.44
6.6.6.6
17.17.17.17
88.88.88.88


As a first step, I tried grep to extract the addresses as follows:



grep -E 'd+.d+.d+.d+' myfile.txt > newfile.txt


But it does not print anything.







share|improve this question






















  • Should 'output to contains only unique umbers and no duplicates' read 'output to contain only unique addresses (i.e. no duplicates)'?
    – maxschlepzig
    Aug 27 at 18:52










  • Thanks. Yes. But that is obvious now to pipe with and sort using: | sort -u. I will remove it form the questions though.
    – user9371654
    Aug 28 at 8:01














up vote
3
down vote

favorite
1












I have a file with the following input. The numbers separated by dots represents addresses. Any number in address can be one or more digits as follows:



[112.112.112.112;3.3.3.3;44.44.44.44]
[6.6.6.6;17.17.17.17;88.88.88.88]


I want to extract each address without the semicolons and brackets (addresses are separated by semicolon ;), and insert each address in a line in a new file to produce this output:



112.112.112.112
3.3.3.3
44.44.44.44
6.6.6.6
17.17.17.17
88.88.88.88


As a first step, I tried grep to extract the addresses as follows:



grep -E 'd+.d+.d+.d+' myfile.txt > newfile.txt


But it does not print anything.







share|improve this question






















  • Should 'output to contains only unique umbers and no duplicates' read 'output to contain only unique addresses (i.e. no duplicates)'?
    – maxschlepzig
    Aug 27 at 18:52










  • Thanks. Yes. But that is obvious now to pipe with and sort using: | sort -u. I will remove it form the questions though.
    – user9371654
    Aug 28 at 8:01












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





I have a file with the following input. The numbers separated by dots represents addresses. Any number in address can be one or more digits as follows:



[112.112.112.112;3.3.3.3;44.44.44.44]
[6.6.6.6;17.17.17.17;88.88.88.88]


I want to extract each address without the semicolons and brackets (addresses are separated by semicolon ;), and insert each address in a line in a new file to produce this output:



112.112.112.112
3.3.3.3
44.44.44.44
6.6.6.6
17.17.17.17
88.88.88.88


As a first step, I tried grep to extract the addresses as follows:



grep -E 'd+.d+.d+.d+' myfile.txt > newfile.txt


But it does not print anything.







share|improve this question














I have a file with the following input. The numbers separated by dots represents addresses. Any number in address can be one or more digits as follows:



[112.112.112.112;3.3.3.3;44.44.44.44]
[6.6.6.6;17.17.17.17;88.88.88.88]


I want to extract each address without the semicolons and brackets (addresses are separated by semicolon ;), and insert each address in a line in a new file to produce this output:



112.112.112.112
3.3.3.3
44.44.44.44
6.6.6.6
17.17.17.17
88.88.88.88


As a first step, I tried grep to extract the addresses as follows:



grep -E 'd+.d+.d+.d+' myfile.txt > newfile.txt


But it does not print anything.









share|improve this question













share|improve this question




share|improve this question








edited Aug 28 at 8:02

























asked Aug 27 at 9:26









user9371654

1226




1226











  • Should 'output to contains only unique umbers and no duplicates' read 'output to contain only unique addresses (i.e. no duplicates)'?
    – maxschlepzig
    Aug 27 at 18:52










  • Thanks. Yes. But that is obvious now to pipe with and sort using: | sort -u. I will remove it form the questions though.
    – user9371654
    Aug 28 at 8:01
















  • Should 'output to contains only unique umbers and no duplicates' read 'output to contain only unique addresses (i.e. no duplicates)'?
    – maxschlepzig
    Aug 27 at 18:52










  • Thanks. Yes. But that is obvious now to pipe with and sort using: | sort -u. I will remove it form the questions though.
    – user9371654
    Aug 28 at 8:01















Should 'output to contains only unique umbers and no duplicates' read 'output to contain only unique addresses (i.e. no duplicates)'?
– maxschlepzig
Aug 27 at 18:52




Should 'output to contains only unique umbers and no duplicates' read 'output to contain only unique addresses (i.e. no duplicates)'?
– maxschlepzig
Aug 27 at 18:52












Thanks. Yes. But that is obvious now to pipe with and sort using: | sort -u. I will remove it form the questions though.
– user9371654
Aug 28 at 8:01




Thanks. Yes. But that is obvious now to pipe with and sort using: | sort -u. I will remove it form the questions though.
– user9371654
Aug 28 at 8:01










4 Answers
4






active

oldest

votes

















up vote
5
down vote



accepted










Extended Regex (-E or egrep) does not know about d. Use -P as suggested by @Alexander or use -E with [0-9] or [[:digit:]] instead.



Add -o to select the matches only instead of whole matching lines. This will also break up the single matches into new lines.



grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+' myfile.txt


or



grep -Eo '[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.[[:digit:]]+' myfile.txt



using Perl Regex (-P or pgrep):



grep -Po 'd+.d+.d+.d+' myfile.txt



If you change + to * you can also use Basic Regex:



grep -o '[0-9]*.[0-9]*.[0-9]*.[0-9]*' myfile.txt





share|improve this answer






















  • I liked this answer as it expalined what's -o for and why -E does not work. But I will use grep -P -o 'd+.d+.d+.d+' myfile.txt as it is more readable. So can you add it so I accept the answer please?
    – user9371654
    Aug 27 at 9:41


















up vote
3
down vote













Replace -E with -P and add -o:



 grep -P -o 'd+.d+.d+.d+' myfile.txt





share|improve this answer





























    up vote
    2
    down vote













    Using awk:



    awk 'NF' RS='[;]' infile


    Or with tr if you don't mind the first empty line:



    tr -s '];[' 'n' <infile





    share|improve this answer





























      up vote
      0
      down vote













      grep is kind of overkill for this task. tr is sufficient:



      $ < input.txt tr -d '' | tr ';' 'n' | sort -u


      The sort -u part removes duplicate addresses.






      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%2f465028%2fextract-addresses-separated-by-semicolon-and-print-each-address-in-a-line%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
        5
        down vote



        accepted










        Extended Regex (-E or egrep) does not know about d. Use -P as suggested by @Alexander or use -E with [0-9] or [[:digit:]] instead.



        Add -o to select the matches only instead of whole matching lines. This will also break up the single matches into new lines.



        grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+' myfile.txt


        or



        grep -Eo '[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.[[:digit:]]+' myfile.txt



        using Perl Regex (-P or pgrep):



        grep -Po 'd+.d+.d+.d+' myfile.txt



        If you change + to * you can also use Basic Regex:



        grep -o '[0-9]*.[0-9]*.[0-9]*.[0-9]*' myfile.txt





        share|improve this answer






















        • I liked this answer as it expalined what's -o for and why -E does not work. But I will use grep -P -o 'd+.d+.d+.d+' myfile.txt as it is more readable. So can you add it so I accept the answer please?
          – user9371654
          Aug 27 at 9:41















        up vote
        5
        down vote



        accepted










        Extended Regex (-E or egrep) does not know about d. Use -P as suggested by @Alexander or use -E with [0-9] or [[:digit:]] instead.



        Add -o to select the matches only instead of whole matching lines. This will also break up the single matches into new lines.



        grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+' myfile.txt


        or



        grep -Eo '[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.[[:digit:]]+' myfile.txt



        using Perl Regex (-P or pgrep):



        grep -Po 'd+.d+.d+.d+' myfile.txt



        If you change + to * you can also use Basic Regex:



        grep -o '[0-9]*.[0-9]*.[0-9]*.[0-9]*' myfile.txt





        share|improve this answer






















        • I liked this answer as it expalined what's -o for and why -E does not work. But I will use grep -P -o 'd+.d+.d+.d+' myfile.txt as it is more readable. So can you add it so I accept the answer please?
          – user9371654
          Aug 27 at 9:41













        up vote
        5
        down vote



        accepted







        up vote
        5
        down vote



        accepted






        Extended Regex (-E or egrep) does not know about d. Use -P as suggested by @Alexander or use -E with [0-9] or [[:digit:]] instead.



        Add -o to select the matches only instead of whole matching lines. This will also break up the single matches into new lines.



        grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+' myfile.txt


        or



        grep -Eo '[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.[[:digit:]]+' myfile.txt



        using Perl Regex (-P or pgrep):



        grep -Po 'd+.d+.d+.d+' myfile.txt



        If you change + to * you can also use Basic Regex:



        grep -o '[0-9]*.[0-9]*.[0-9]*.[0-9]*' myfile.txt





        share|improve this answer














        Extended Regex (-E or egrep) does not know about d. Use -P as suggested by @Alexander or use -E with [0-9] or [[:digit:]] instead.



        Add -o to select the matches only instead of whole matching lines. This will also break up the single matches into new lines.



        grep -Eo '[0-9]+.[0-9]+.[0-9]+.[0-9]+' myfile.txt


        or



        grep -Eo '[[:digit:]]+.[[:digit:]]+.[[:digit:]]+.[[:digit:]]+' myfile.txt



        using Perl Regex (-P or pgrep):



        grep -Po 'd+.d+.d+.d+' myfile.txt



        If you change + to * you can also use Basic Regex:



        grep -o '[0-9]*.[0-9]*.[0-9]*.[0-9]*' myfile.txt






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 27 at 11:41

























        answered Aug 27 at 9:33









        RoVo

        1,234210




        1,234210











        • I liked this answer as it expalined what's -o for and why -E does not work. But I will use grep -P -o 'd+.d+.d+.d+' myfile.txt as it is more readable. So can you add it so I accept the answer please?
          – user9371654
          Aug 27 at 9:41

















        • I liked this answer as it expalined what's -o for and why -E does not work. But I will use grep -P -o 'd+.d+.d+.d+' myfile.txt as it is more readable. So can you add it so I accept the answer please?
          – user9371654
          Aug 27 at 9:41
















        I liked this answer as it expalined what's -o for and why -E does not work. But I will use grep -P -o 'd+.d+.d+.d+' myfile.txt as it is more readable. So can you add it so I accept the answer please?
        – user9371654
        Aug 27 at 9:41





        I liked this answer as it expalined what's -o for and why -E does not work. But I will use grep -P -o 'd+.d+.d+.d+' myfile.txt as it is more readable. So can you add it so I accept the answer please?
        – user9371654
        Aug 27 at 9:41













        up vote
        3
        down vote













        Replace -E with -P and add -o:



         grep -P -o 'd+.d+.d+.d+' myfile.txt





        share|improve this answer


























          up vote
          3
          down vote













          Replace -E with -P and add -o:



           grep -P -o 'd+.d+.d+.d+' myfile.txt





          share|improve this answer
























            up vote
            3
            down vote










            up vote
            3
            down vote









            Replace -E with -P and add -o:



             grep -P -o 'd+.d+.d+.d+' myfile.txt





            share|improve this answer














            Replace -E with -P and add -o:



             grep -P -o 'd+.d+.d+.d+' myfile.txt






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 27 at 9:55









            GAD3R

            22.7k154895




            22.7k154895










            answered Aug 27 at 9:30









            Alexander

            65012




            65012




















                up vote
                2
                down vote













                Using awk:



                awk 'NF' RS='[;]' infile


                Or with tr if you don't mind the first empty line:



                tr -s '];[' 'n' <infile





                share|improve this answer


























                  up vote
                  2
                  down vote













                  Using awk:



                  awk 'NF' RS='[;]' infile


                  Or with tr if you don't mind the first empty line:



                  tr -s '];[' 'n' <infile





                  share|improve this answer
























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    Using awk:



                    awk 'NF' RS='[;]' infile


                    Or with tr if you don't mind the first empty line:



                    tr -s '];[' 'n' <infile





                    share|improve this answer














                    Using awk:



                    awk 'NF' RS='[;]' infile


                    Or with tr if you don't mind the first empty line:



                    tr -s '];[' 'n' <infile






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 27 at 14:13

























                    answered Aug 27 at 9:30









                    αғsнιη

                    15.6k92563




                    15.6k92563




















                        up vote
                        0
                        down vote













                        grep is kind of overkill for this task. tr is sufficient:



                        $ < input.txt tr -d '' | tr ';' 'n' | sort -u


                        The sort -u part removes duplicate addresses.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          grep is kind of overkill for this task. tr is sufficient:



                          $ < input.txt tr -d '' | tr ';' 'n' | sort -u


                          The sort -u part removes duplicate addresses.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            grep is kind of overkill for this task. tr is sufficient:



                            $ < input.txt tr -d '' | tr ';' 'n' | sort -u


                            The sort -u part removes duplicate addresses.






                            share|improve this answer












                            grep is kind of overkill for this task. tr is sufficient:



                            $ < input.txt tr -d '' | tr ';' 'n' | sort -u


                            The sort -u part removes duplicate addresses.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 27 at 18:50









                            maxschlepzig

                            32k30135202




                            32k30135202



























                                 

                                draft saved


                                draft discarded















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465028%2fextract-addresses-separated-by-semicolon-and-print-each-address-in-a-line%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                What does second last employer means? [closed]

                                List of Gilmore Girls characters

                                One-line joke