copy certain spaces from a file












1














I have a file which looks like this



   18DMA      H 9996   0.886   5.687   5.320
18DMA H 9997 1.019 5.764 5.247
18DMA Np 9998 0.947 5.584 5.151
18DMA H 9999 1.033 5.541 5.113
18DMA Cn10000 0.880 5.674 5.050
18DMA H10001 0.831 5.616 4.971
18DMA H10002 0.814 5.751 5.091
18DMA H10003 0.957 5.735 5.003
18DMA Cn10004 0.837 5.486 5.185


The desire output is to delete column 3 however since from a certain row/line and next there is no a space between atom name and number I cannot make the deletion by column. Is there any way to make the deletion by selecting certain number of characters? The desire output should be



   18DMA      H    0.886   5.687   5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185









share|improve this question
























  • clarify your atom name and number
    – RomanPerekhrest
    1 hour ago










  • My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
    – Dimitris Mintis
    1 hour ago
















1














I have a file which looks like this



   18DMA      H 9996   0.886   5.687   5.320
18DMA H 9997 1.019 5.764 5.247
18DMA Np 9998 0.947 5.584 5.151
18DMA H 9999 1.033 5.541 5.113
18DMA Cn10000 0.880 5.674 5.050
18DMA H10001 0.831 5.616 4.971
18DMA H10002 0.814 5.751 5.091
18DMA H10003 0.957 5.735 5.003
18DMA Cn10004 0.837 5.486 5.185


The desire output is to delete column 3 however since from a certain row/line and next there is no a space between atom name and number I cannot make the deletion by column. Is there any way to make the deletion by selecting certain number of characters? The desire output should be



   18DMA      H    0.886   5.687   5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185









share|improve this question
























  • clarify your atom name and number
    – RomanPerekhrest
    1 hour ago










  • My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
    – Dimitris Mintis
    1 hour ago














1












1








1







I have a file which looks like this



   18DMA      H 9996   0.886   5.687   5.320
18DMA H 9997 1.019 5.764 5.247
18DMA Np 9998 0.947 5.584 5.151
18DMA H 9999 1.033 5.541 5.113
18DMA Cn10000 0.880 5.674 5.050
18DMA H10001 0.831 5.616 4.971
18DMA H10002 0.814 5.751 5.091
18DMA H10003 0.957 5.735 5.003
18DMA Cn10004 0.837 5.486 5.185


The desire output is to delete column 3 however since from a certain row/line and next there is no a space between atom name and number I cannot make the deletion by column. Is there any way to make the deletion by selecting certain number of characters? The desire output should be



   18DMA      H    0.886   5.687   5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185









share|improve this question















I have a file which looks like this



   18DMA      H 9996   0.886   5.687   5.320
18DMA H 9997 1.019 5.764 5.247
18DMA Np 9998 0.947 5.584 5.151
18DMA H 9999 1.033 5.541 5.113
18DMA Cn10000 0.880 5.674 5.050
18DMA H10001 0.831 5.616 4.971
18DMA H10002 0.814 5.751 5.091
18DMA H10003 0.957 5.735 5.003
18DMA Cn10004 0.837 5.486 5.185


The desire output is to delete column 3 however since from a certain row/line and next there is no a space between atom name and number I cannot make the deletion by column. Is there any way to make the deletion by selecting certain number of characters? The desire output should be



   18DMA      H    0.886   5.687   5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185






text-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









Kusalananda

121k16228372




121k16228372










asked 1 hour ago









Dimitris Mintis

475




475












  • clarify your atom name and number
    – RomanPerekhrest
    1 hour ago










  • My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
    – Dimitris Mintis
    1 hour ago


















  • clarify your atom name and number
    – RomanPerekhrest
    1 hour ago










  • My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
    – Dimitris Mintis
    1 hour ago
















clarify your atom name and number
– RomanPerekhrest
1 hour ago




clarify your atom name and number
– RomanPerekhrest
1 hour ago












My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
– Dimitris Mintis
1 hour ago




My problem is in line 18DMA Cn10000 0.880 5.674 5.050 since there is no space between Cn and 1000 so I cannot proceed with copying the desire column. Somehow I need instead of copying a column to copy certain characters to make it work
– Dimitris Mintis
1 hour ago










3 Answers
3






active

oldest

votes


















2














Assuming you don't have <TAB>s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with



  $ sed -E 's/^(.{15}).{5}/1/' file
18DMA H 0.886 5.687 5.320
18DMA H 1.019 5.764 5.247
18DMA Np 0.947 5.584 5.151
18DMA H 1.033 5.541 5.113
18DMA Cn 0.880 5.674 5.050
18DMA H 0.831 5.616 4.971
18DMA H 0.814 5.751 5.091
18DMA H 0.957 5.735 5.003
18DMA Cn 0.837 5.486 5.185


It's using a "back reference" for the first 15 characters to restore them using 1
in the replacement part of the substitute command.






share|improve this answer





















  • super!! many thanks!
    – Dimitris Mintis
    1 hour ago



















2














Use cut in character mode:



cut -c1-15,21-


you may need to tweak the exact character numbers.
Again, this assumes the input doesn't use TABs (t characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).






share|improve this answer





























    2














    $ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
    18DMA H 0.886 5.687 5.320
    18DMA H 1.019 5.764 5.247
    18DMA Np 0.947 5.584 5.151
    18DMA H 1.033 5.541 5.113
    18DMA Cn 0.880 5.674 5.050
    18DMA H 0.831 5.616 4.971
    18DMA H 0.814 5.751 5.091
    18DMA H 0.957 5.735 5.003
    18DMA Cn 0.837 5.486 5.185


    This short awk program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.



    If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.



    The output will be tab-delimited (or delimited by whatever you set OFS to on the command line).






    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',
      autoActivateHeartbeat: false,
      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%2f490597%2fcopy-certain-spaces-from-a-file%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Assuming you don't have <TAB>s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with



        $ sed -E 's/^(.{15}).{5}/1/' file
      18DMA H 0.886 5.687 5.320
      18DMA H 1.019 5.764 5.247
      18DMA Np 0.947 5.584 5.151
      18DMA H 1.033 5.541 5.113
      18DMA Cn 0.880 5.674 5.050
      18DMA H 0.831 5.616 4.971
      18DMA H 0.814 5.751 5.091
      18DMA H 0.957 5.735 5.003
      18DMA Cn 0.837 5.486 5.185


      It's using a "back reference" for the first 15 characters to restore them using 1
      in the replacement part of the substitute command.






      share|improve this answer





















      • super!! many thanks!
        – Dimitris Mintis
        1 hour ago
















      2














      Assuming you don't have <TAB>s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with



        $ sed -E 's/^(.{15}).{5}/1/' file
      18DMA H 0.886 5.687 5.320
      18DMA H 1.019 5.764 5.247
      18DMA Np 0.947 5.584 5.151
      18DMA H 1.033 5.541 5.113
      18DMA Cn 0.880 5.674 5.050
      18DMA H 0.831 5.616 4.971
      18DMA H 0.814 5.751 5.091
      18DMA H 0.957 5.735 5.003
      18DMA Cn 0.837 5.486 5.185


      It's using a "back reference" for the first 15 characters to restore them using 1
      in the replacement part of the substitute command.






      share|improve this answer





















      • super!! many thanks!
        – Dimitris Mintis
        1 hour ago














      2












      2








      2






      Assuming you don't have <TAB>s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with



        $ sed -E 's/^(.{15}).{5}/1/' file
      18DMA H 0.886 5.687 5.320
      18DMA H 1.019 5.764 5.247
      18DMA Np 0.947 5.584 5.151
      18DMA H 1.033 5.541 5.113
      18DMA Cn 0.880 5.674 5.050
      18DMA H 0.831 5.616 4.971
      18DMA H 0.814 5.751 5.091
      18DMA H 0.957 5.735 5.003
      18DMA Cn 0.837 5.486 5.185


      It's using a "back reference" for the first 15 characters to restore them using 1
      in the replacement part of the substitute command.






      share|improve this answer












      Assuming you don't have <TAB>s but multiple spaces as field separators, and by looking at and counting your sample data, I came up with



        $ sed -E 's/^(.{15}).{5}/1/' file
      18DMA H 0.886 5.687 5.320
      18DMA H 1.019 5.764 5.247
      18DMA Np 0.947 5.584 5.151
      18DMA H 1.033 5.541 5.113
      18DMA Cn 0.880 5.674 5.050
      18DMA H 0.831 5.616 4.971
      18DMA H 0.814 5.751 5.091
      18DMA H 0.957 5.735 5.003
      18DMA Cn 0.837 5.486 5.185


      It's using a "back reference" for the first 15 characters to restore them using 1
      in the replacement part of the substitute command.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered 1 hour ago









      RudiC

      3,9941312




      3,9941312












      • super!! many thanks!
        – Dimitris Mintis
        1 hour ago


















      • super!! many thanks!
        – Dimitris Mintis
        1 hour ago
















      super!! many thanks!
      – Dimitris Mintis
      1 hour ago




      super!! many thanks!
      – Dimitris Mintis
      1 hour ago













      2














      Use cut in character mode:



      cut -c1-15,21-


      you may need to tweak the exact character numbers.
      Again, this assumes the input doesn't use TABs (t characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).






      share|improve this answer


























        2














        Use cut in character mode:



        cut -c1-15,21-


        you may need to tweak the exact character numbers.
        Again, this assumes the input doesn't use TABs (t characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).






        share|improve this answer
























          2












          2








          2






          Use cut in character mode:



          cut -c1-15,21-


          you may need to tweak the exact character numbers.
          Again, this assumes the input doesn't use TABs (t characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).






          share|improve this answer












          Use cut in character mode:



          cut -c1-15,21-


          you may need to tweak the exact character numbers.
          Again, this assumes the input doesn't use TABs (t characters) as delimiters (which it probably doesn't, since then you wouldn't have the problem of the joined fields in the first place).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          peterph

          23k24457




          23k24457























              2














              $ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
              18DMA H 0.886 5.687 5.320
              18DMA H 1.019 5.764 5.247
              18DMA Np 0.947 5.584 5.151
              18DMA H 1.033 5.541 5.113
              18DMA Cn 0.880 5.674 5.050
              18DMA H 0.831 5.616 4.971
              18DMA H 0.814 5.751 5.091
              18DMA H 0.957 5.735 5.003
              18DMA Cn 0.837 5.486 5.185


              This short awk program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.



              If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.



              The output will be tab-delimited (or delimited by whatever you set OFS to on the command line).






              share|improve this answer




























                2














                $ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
                18DMA H 0.886 5.687 5.320
                18DMA H 1.019 5.764 5.247
                18DMA Np 0.947 5.584 5.151
                18DMA H 1.033 5.541 5.113
                18DMA Cn 0.880 5.674 5.050
                18DMA H 0.831 5.616 4.971
                18DMA H 0.814 5.751 5.091
                18DMA H 0.957 5.735 5.003
                18DMA Cn 0.837 5.486 5.185


                This short awk program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.



                If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.



                The output will be tab-delimited (or delimited by whatever you set OFS to on the command line).






                share|improve this answer


























                  2












                  2








                  2






                  $ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
                  18DMA H 0.886 5.687 5.320
                  18DMA H 1.019 5.764 5.247
                  18DMA Np 0.947 5.584 5.151
                  18DMA H 1.033 5.541 5.113
                  18DMA Cn 0.880 5.674 5.050
                  18DMA H 0.831 5.616 4.971
                  18DMA H 0.814 5.751 5.091
                  18DMA H 0.957 5.735 5.003
                  18DMA Cn 0.837 5.486 5.185


                  This short awk program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.



                  If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.



                  The output will be tab-delimited (or delimited by whatever you set OFS to on the command line).






                  share|improve this answer














                  $ awk -v OFS='t' 'NF == 5 { sub("[0-9]*$", "", $2) } NF == 6 { $0 = $1 OFS $2 OFS $4 OFS $5 OFS $6 } { print }' file
                  18DMA H 0.886 5.687 5.320
                  18DMA H 1.019 5.764 5.247
                  18DMA Np 0.947 5.584 5.151
                  18DMA H 1.033 5.541 5.113
                  18DMA Cn 0.880 5.674 5.050
                  18DMA H 0.831 5.616 4.971
                  18DMA H 0.814 5.751 5.091
                  18DMA H 0.957 5.735 5.003
                  18DMA Cn 0.837 5.486 5.185


                  This short awk program will do different things to the input line depending on whether it contains 5 or 6 whitespace-delimited fields.



                  If it contains five fields, it removes all digits from the end of the second fields and leaves the rest as it is. If it contains six fields, it rewrites the line but omits the third field.



                  The output will be tab-delimited (or delimited by whatever you set OFS to on the command line).







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 1 hour ago

























                  answered 1 hour ago









                  Kusalananda

                  121k16228372




                  121k16228372






























                      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%2f490597%2fcopy-certain-spaces-from-a-file%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