Replace all but a set of characters in a file with newline











up vote
2
down vote

favorite












how to delete all characters in file except numbers and "." ,
each word (numbers/dot) should be in new line in file see example2




  • the solution can be with sed or awk or ksh syntax


remark - the solution must be according to the example 2



example 1



file before edit



  192.0.22.1++0.1
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
3.3.3ki.3.


example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line



  192.0.22.1
0.1
32 5.500.5.5
1.1.1.1
1.1.1.1333
33331.1.1.1
5.5.5.
.
5.5.5.5
1.1.1.13444
54
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
5.4.3.277
3.3.3 .3.









share|improve this question




















  • 1




    Your sample output is a bit inconsistent. Why are 32 and 5.500.5.5 both on line 3? Why is there no 3 (from 3de) between the lines for 5.5.5. and .? Why are 3.3.3 and .3. both on the last line?
    – jw013
    Nov 15 '12 at 19:15








  • 2




    This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
    – glenn jackman
    Nov 15 '12 at 21:29















up vote
2
down vote

favorite












how to delete all characters in file except numbers and "." ,
each word (numbers/dot) should be in new line in file see example2




  • the solution can be with sed or awk or ksh syntax


remark - the solution must be according to the example 2



example 1



file before edit



  192.0.22.1++0.1
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
3.3.3ki.3.


example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line



  192.0.22.1
0.1
32 5.500.5.5
1.1.1.1
1.1.1.1333
33331.1.1.1
5.5.5.
.
5.5.5.5
1.1.1.13444
54
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
5.4.3.277
3.3.3 .3.









share|improve this question




















  • 1




    Your sample output is a bit inconsistent. Why are 32 and 5.500.5.5 both on line 3? Why is there no 3 (from 3de) between the lines for 5.5.5. and .? Why are 3.3.3 and .3. both on the last line?
    – jw013
    Nov 15 '12 at 19:15








  • 2




    This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
    – glenn jackman
    Nov 15 '12 at 21:29













up vote
2
down vote

favorite









up vote
2
down vote

favorite











how to delete all characters in file except numbers and "." ,
each word (numbers/dot) should be in new line in file see example2




  • the solution can be with sed or awk or ksh syntax


remark - the solution must be according to the example 2



example 1



file before edit



  192.0.22.1++0.1
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
3.3.3ki.3.


example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line



  192.0.22.1
0.1
32 5.500.5.5
1.1.1.1
1.1.1.1333
33331.1.1.1
5.5.5.
.
5.5.5.5
1.1.1.13444
54
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
5.4.3.277
3.3.3 .3.









share|improve this question















how to delete all characters in file except numbers and "." ,
each word (numbers/dot) should be in new line in file see example2




  • the solution can be with sed or awk or ksh syntax


remark - the solution must be according to the example 2



example 1



file before edit



  192.0.22.1++0.1
e32)5.500.5.5*kjcdr
##@$1.1.1.1+++jmjh
1.1.1.1333
33331.1.1.1
@5.5.5.??????
~3de.ede5.5.5.5
1.1.1.13444r54
192.9.30.174
&&^#%5.5.5.5
:5.5.5.5@%%^^&*
:5.5.5.5:
**22.22.22.22
172.78.0.1()*5.4.3.277
3.3.3ki.3.


example 2 of file after delete all characters except numbers and "." charter , each new word will be in new line



  192.0.22.1
0.1
32 5.500.5.5
1.1.1.1
1.1.1.1333
33331.1.1.1
5.5.5.
.
5.5.5.5
1.1.1.13444
54
192.9.30.174
5.5.5.5
5.5.5.5
5.5.5.5
22.22.22.22
172.78.0.1
5.4.3.277
3.3.3 .3.






linux sed awk perl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '12 at 19:22









jw013

35.9k699125




35.9k699125










asked Nov 15 '12 at 12:54









yael

52261631




52261631








  • 1




    Your sample output is a bit inconsistent. Why are 32 and 5.500.5.5 both on line 3? Why is there no 3 (from 3de) between the lines for 5.5.5. and .? Why are 3.3.3 and .3. both on the last line?
    – jw013
    Nov 15 '12 at 19:15








  • 2




    This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
    – glenn jackman
    Nov 15 '12 at 21:29














  • 1




    Your sample output is a bit inconsistent. Why are 32 and 5.500.5.5 both on line 3? Why is there no 3 (from 3de) between the lines for 5.5.5. and .? Why are 3.3.3 and .3. both on the last line?
    – jw013
    Nov 15 '12 at 19:15








  • 2




    This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
    – glenn jackman
    Nov 15 '12 at 21:29








1




1




Your sample output is a bit inconsistent. Why are 32 and 5.500.5.5 both on line 3? Why is there no 3 (from 3de) between the lines for 5.5.5. and .? Why are 3.3.3 and .3. both on the last line?
– jw013
Nov 15 '12 at 19:15






Your sample output is a bit inconsistent. Why are 32 and 5.500.5.5 both on line 3? Why is there no 3 (from 3de) between the lines for 5.5.5. and .? Why are 3.3.3 and .3. both on the last line?
– jw013
Nov 15 '12 at 19:15






2




2




This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
– glenn jackman
Nov 15 '12 at 21:29




This looks an awful lot like this question on ServerFault -- did you not get a good answer there?
– glenn jackman
Nov 15 '12 at 21:29










5 Answers
5






active

oldest

votes

















up vote
4
down vote



accepted










This is a classic tr use case, so the simplest way is:



tr -cs '[:digit:].' '[n*]' < input > output


The [:digit:]. argument specifies the characters to match (digits and dot). The [n*] specifies the characters to replace with (replace everything with newline). The -c option inverts the first argument since we want everything except digits and dot. The -s squeezes consecutive newlines from the second string into one.






share|improve this answer




























    up vote
    1
    down vote













    grep can do it:



    grep -o '[0-9.]+'





    share|improve this answer




























      up vote
      0
      down vote













      You can use sed to replace any unwanted character to a newline, and then grep to get rid of empty lines:



      sed 's/[^0-9.]/n/g' | grep . 


      Note that the result is different from the one you posted: 32 5.500.5.5 is split to two lines, as well as the last line.



      Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.



      perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'





      share|improve this answer

















      • 1




        Note that n above in the sed command example is not standard. The standard syntax to specify a newline character in the RHS of a s command is with a backslash followed by a new line character.
        – Stéphane Chazelas
        Nov 15 '12 at 19:39


















      up vote
      0
      down vote













      Here's one way with GNU sed:



      sed ':a;{N;s/[^.0-9]+/n/g};ba' file


      Here's how it works:




      1. Create a branch label;

      2. Append current/next line to register;

      3. Branch if not last line;

      4. Replace all groups that aren't matching with newlines.


      Using the branch avoids spurious newlines.






      share|improve this answer






























        up vote
        0
        down vote













        on RHEL7:



        replace any line starting with VALUETOEDIT



        sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile





        share|improve this answer










        New contributor




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


















          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          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%2f55812%2freplace-all-but-a-set-of-characters-in-a-file-with-newline%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          This is a classic tr use case, so the simplest way is:



          tr -cs '[:digit:].' '[n*]' < input > output


          The [:digit:]. argument specifies the characters to match (digits and dot). The [n*] specifies the characters to replace with (replace everything with newline). The -c option inverts the first argument since we want everything except digits and dot. The -s squeezes consecutive newlines from the second string into one.






          share|improve this answer

























            up vote
            4
            down vote



            accepted










            This is a classic tr use case, so the simplest way is:



            tr -cs '[:digit:].' '[n*]' < input > output


            The [:digit:]. argument specifies the characters to match (digits and dot). The [n*] specifies the characters to replace with (replace everything with newline). The -c option inverts the first argument since we want everything except digits and dot. The -s squeezes consecutive newlines from the second string into one.






            share|improve this answer























              up vote
              4
              down vote



              accepted







              up vote
              4
              down vote



              accepted






              This is a classic tr use case, so the simplest way is:



              tr -cs '[:digit:].' '[n*]' < input > output


              The [:digit:]. argument specifies the characters to match (digits and dot). The [n*] specifies the characters to replace with (replace everything with newline). The -c option inverts the first argument since we want everything except digits and dot. The -s squeezes consecutive newlines from the second string into one.






              share|improve this answer












              This is a classic tr use case, so the simplest way is:



              tr -cs '[:digit:].' '[n*]' < input > output


              The [:digit:]. argument specifies the characters to match (digits and dot). The [n*] specifies the characters to replace with (replace everything with newline). The -c option inverts the first argument since we want everything except digits and dot. The -s squeezes consecutive newlines from the second string into one.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '12 at 19:19









              jw013

              35.9k699125




              35.9k699125
























                  up vote
                  1
                  down vote













                  grep can do it:



                  grep -o '[0-9.]+'





                  share|improve this answer

























                    up vote
                    1
                    down vote













                    grep can do it:



                    grep -o '[0-9.]+'





                    share|improve this answer























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      grep can do it:



                      grep -o '[0-9.]+'





                      share|improve this answer












                      grep can do it:



                      grep -o '[0-9.]+'






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '12 at 21:24









                      glenn jackman

                      50k569106




                      50k569106






















                          up vote
                          0
                          down vote













                          You can use sed to replace any unwanted character to a newline, and then grep to get rid of empty lines:



                          sed 's/[^0-9.]/n/g' | grep . 


                          Note that the result is different from the one you posted: 32 5.500.5.5 is split to two lines, as well as the last line.



                          Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.



                          perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'





                          share|improve this answer

















                          • 1




                            Note that n above in the sed command example is not standard. The standard syntax to specify a newline character in the RHS of a s command is with a backslash followed by a new line character.
                            – Stéphane Chazelas
                            Nov 15 '12 at 19:39















                          up vote
                          0
                          down vote













                          You can use sed to replace any unwanted character to a newline, and then grep to get rid of empty lines:



                          sed 's/[^0-9.]/n/g' | grep . 


                          Note that the result is different from the one you posted: 32 5.500.5.5 is split to two lines, as well as the last line.



                          Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.



                          perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'





                          share|improve this answer

















                          • 1




                            Note that n above in the sed command example is not standard. The standard syntax to specify a newline character in the RHS of a s command is with a backslash followed by a new line character.
                            – Stéphane Chazelas
                            Nov 15 '12 at 19:39













                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You can use sed to replace any unwanted character to a newline, and then grep to get rid of empty lines:



                          sed 's/[^0-9.]/n/g' | grep . 


                          Note that the result is different from the one you posted: 32 5.500.5.5 is split to two lines, as well as the last line.



                          Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.



                          perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'





                          share|improve this answer












                          You can use sed to replace any unwanted character to a newline, and then grep to get rid of empty lines:



                          sed 's/[^0-9.]/n/g' | grep . 


                          Note that the result is different from the one you posted: 32 5.500.5.5 is split to two lines, as well as the last line.



                          Perl solution: it splits each line on unwanted characters, and greps for nonempty lines.



                          perl -ne 'print "$_n" for grep /./, split /[^0-9.]+/'






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '12 at 13:05









                          choroba

                          26.2k44571




                          26.2k44571








                          • 1




                            Note that n above in the sed command example is not standard. The standard syntax to specify a newline character in the RHS of a s command is with a backslash followed by a new line character.
                            – Stéphane Chazelas
                            Nov 15 '12 at 19:39














                          • 1




                            Note that n above in the sed command example is not standard. The standard syntax to specify a newline character in the RHS of a s command is with a backslash followed by a new line character.
                            – Stéphane Chazelas
                            Nov 15 '12 at 19:39








                          1




                          1




                          Note that n above in the sed command example is not standard. The standard syntax to specify a newline character in the RHS of a s command is with a backslash followed by a new line character.
                          – Stéphane Chazelas
                          Nov 15 '12 at 19:39




                          Note that n above in the sed command example is not standard. The standard syntax to specify a newline character in the RHS of a s command is with a backslash followed by a new line character.
                          – Stéphane Chazelas
                          Nov 15 '12 at 19:39










                          up vote
                          0
                          down vote













                          Here's one way with GNU sed:



                          sed ':a;{N;s/[^.0-9]+/n/g};ba' file


                          Here's how it works:




                          1. Create a branch label;

                          2. Append current/next line to register;

                          3. Branch if not last line;

                          4. Replace all groups that aren't matching with newlines.


                          Using the branch avoids spurious newlines.






                          share|improve this answer



























                            up vote
                            0
                            down vote













                            Here's one way with GNU sed:



                            sed ':a;{N;s/[^.0-9]+/n/g};ba' file


                            Here's how it works:




                            1. Create a branch label;

                            2. Append current/next line to register;

                            3. Branch if not last line;

                            4. Replace all groups that aren't matching with newlines.


                            Using the branch avoids spurious newlines.






                            share|improve this answer

























                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              Here's one way with GNU sed:



                              sed ':a;{N;s/[^.0-9]+/n/g};ba' file


                              Here's how it works:




                              1. Create a branch label;

                              2. Append current/next line to register;

                              3. Branch if not last line;

                              4. Replace all groups that aren't matching with newlines.


                              Using the branch avoids spurious newlines.






                              share|improve this answer














                              Here's one way with GNU sed:



                              sed ':a;{N;s/[^.0-9]+/n/g};ba' file


                              Here's how it works:




                              1. Create a branch label;

                              2. Append current/next line to register;

                              3. Branch if not last line;

                              4. Replace all groups that aren't matching with newlines.


                              Using the branch avoids spurious newlines.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 15 '12 at 19:36









                              Stéphane Chazelas

                              297k54562908




                              297k54562908










                              answered Nov 15 '12 at 13:12









                              Chris Down

                              78.7k13188201




                              78.7k13188201






















                                  up vote
                                  0
                                  down vote













                                  on RHEL7:



                                  replace any line starting with VALUETOEDIT



                                  sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile





                                  share|improve this answer










                                  New contributor




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






















                                    up vote
                                    0
                                    down vote













                                    on RHEL7:



                                    replace any line starting with VALUETOEDIT



                                    sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile





                                    share|improve this answer










                                    New contributor




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




















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      on RHEL7:



                                      replace any line starting with VALUETOEDIT



                                      sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile





                                      share|improve this answer










                                      New contributor




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









                                      on RHEL7:



                                      replace any line starting with VALUETOEDIT



                                      sed -i -e 's/^VALUETOEDIT.*/NEWVALUE/g' somefile






                                      share|improve this answer










                                      New contributor




                                      surilin3 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 yesterday









                                      jimmij

                                      30.6k869103




                                      30.6k869103






                                      New contributor




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









                                      answered yesterday









                                      surilin3

                                      1




                                      1




                                      New contributor




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





                                      New contributor





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






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






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f55812%2freplace-all-but-a-set-of-characters-in-a-file-with-newline%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Accessing regular linux commands in Huawei's Dopra Linux

                                          Can't connect RFCOMM socket: Host is down

                                          Kernel panic - not syncing: Fatal Exception in Interrupt