How to split file based on number of columns? Announcing the arrival of Valued Associate #679:...

What is the meaning of the new sigil in Game of Thrones Season 8 intro?

Why did the rest of the Eastern Bloc not invade Yugoslavia?

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

Is it true that "carbohydrates are of no use for the basal metabolic need"?

Overriding an object in memory with placement new

Identifying polygons that intersect with another layer using QGIS?

How to run gsettings for another user Ubuntu 18.04.2 LTS

Selecting the same column from Different rows Based on Different Criteria

At the end of Thor: Ragnarok why don't the Asgardians turn and head for the Bifrost as per their original plan?

How to call a function with default parameter through a pointer to function that is the return of another function?

Apollo command module space walk?

String `!23` is replaced with `docker` in command line

How can I make names more distinctive without making them longer?

What does the "x" in "x86" represent?

Resolving to minmaj7

Storing hydrofluoric acid before the invention of plastics

If a contract sometimes uses the wrong name, is it still valid?

Seeking colloquialism for “just because”

How to align text above triangle figure

Using audio cues to encourage good posture

Align equal signs while including text over equalities

Why was the term "discrete" used in discrete logarithm?

Can a non-EU citizen traveling with me come with me through the EU passport line?

How to answer "Have you ever been terminated?"



How to split file based on number of columns?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionHow to multiply a data file with another index data file?UNIX paste columns and insert zeros for all missing valuesHandling dynamically changing column positions and splitting fileSplit column into separate columnsSplit a Column in multiple columns according to a specific string according to irregular number of items between each stringSplitting a single file into multiple files based on matching strings in LinuxHow to do multiple splits of a csv file based on unique column values?alter awk variable based on match inside awkHow to split a file into paragraphs and name the resulting pieces based on an identifier present in each paragraphHow to delete columns with zero value in each line?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







1















I have a file containing numeric data. Each line has a varying number of columns. I want to Split the file in to multiple files based on number of columns in a Line. Each Line may have columns varying from 1-10



Below is a sample Input



file.txt
23 53;
34;
31 45 67;
46 78 95;
34 17;
19;
37 65 83;


Target Output



file_1column.txt
34;
19;

file_2column.txt
23 53;
34 17;

file_3column.txt
31 45 67;
46 78 95;
37 65 83;









share|improve this question









New contributor




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



























    1















    I have a file containing numeric data. Each line has a varying number of columns. I want to Split the file in to multiple files based on number of columns in a Line. Each Line may have columns varying from 1-10



    Below is a sample Input



    file.txt
    23 53;
    34;
    31 45 67;
    46 78 95;
    34 17;
    19;
    37 65 83;


    Target Output



    file_1column.txt
    34;
    19;

    file_2column.txt
    23 53;
    34 17;

    file_3column.txt
    31 45 67;
    46 78 95;
    37 65 83;









    share|improve this question









    New contributor




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























      1












      1








      1








      I have a file containing numeric data. Each line has a varying number of columns. I want to Split the file in to multiple files based on number of columns in a Line. Each Line may have columns varying from 1-10



      Below is a sample Input



      file.txt
      23 53;
      34;
      31 45 67;
      46 78 95;
      34 17;
      19;
      37 65 83;


      Target Output



      file_1column.txt
      34;
      19;

      file_2column.txt
      23 53;
      34 17;

      file_3column.txt
      31 45 67;
      46 78 95;
      37 65 83;









      share|improve this question









      New contributor




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












      I have a file containing numeric data. Each line has a varying number of columns. I want to Split the file in to multiple files based on number of columns in a Line. Each Line may have columns varying from 1-10



      Below is a sample Input



      file.txt
      23 53;
      34;
      31 45 67;
      46 78 95;
      34 17;
      19;
      37 65 83;


      Target Output



      file_1column.txt
      34;
      19;

      file_2column.txt
      23 53;
      34 17;

      file_3column.txt
      31 45 67;
      46 78 95;
      37 65 83;






      text-processing awk






      share|improve this question









      New contributor




      rkatraga 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 question









      New contributor




      rkatraga 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 question




      share|improve this question








      edited 5 hours ago









      αғsнιη

      17.2k103069




      17.2k103069






      New contributor




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









      asked 5 hours ago









      rkatragarkatraga

      175




      175




      New contributor




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





      New contributor





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






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






















          1 Answer
          1






          active

          oldest

          votes


















          3














          With awk, creating the desired filenames by concatenating fixed strings "file_" and"column.txt" with the internal variable NF (which contains the number of fields - or columns - in each input record - or line):



          awk '{print > "file_" NF "column.txt"}' file.txt


          Result:



          $ head file_?column.txt
          ==> file_1column.txt <==
          34;
          19;

          ==> file_2column.txt <==
          23 53;
          34 17;

          ==> file_3column.txt <==
          31 45 67;
          46 78 95;
          37 65 83;





          share|improve this answer


























          • It works. But could you explain the command. I didn't understand how it was done. Thank you.

            – rkatraga
            4 hours ago













          • @rkatraga I have added some explanation above

            – steeldriver
            4 hours ago











          • I understand the desired file name part. What I don't understand is how is awk splitting the file based on columns in each line. awk '{print>NF}' file.txt The above command is also doing the job (keeping aside the desired filename), I just don't get how it is splitting? The {print>NF} - What is happening here? I know NF is Number of Fields, but why is Splitting Happening.

            – rkatraga
            4 hours ago








          • 1





            @rkatraga It is more sorting than splitting. awk is processing the file line by line, as usual, and for each line it is executing a program that reads "print the current line to the file that is named after the current line's number of columns (fields)". Think about it as if you were sorting a deck of cards - reading the number printed on each one an putting it on the corresponding pile. (Sorry for the invasion, steeldriver).

            – fra-san
            3 hours ago








          • 1





            Got it. Great Thanks. Its just so beautiful. Can't appreciate enough.

            – rkatraga
            3 hours ago














          Your Answer








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

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

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          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
          });


          }
          });






          rkatraga is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512848%2fhow-to-split-file-based-on-number-of-columns%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          With awk, creating the desired filenames by concatenating fixed strings "file_" and"column.txt" with the internal variable NF (which contains the number of fields - or columns - in each input record - or line):



          awk '{print > "file_" NF "column.txt"}' file.txt


          Result:



          $ head file_?column.txt
          ==> file_1column.txt <==
          34;
          19;

          ==> file_2column.txt <==
          23 53;
          34 17;

          ==> file_3column.txt <==
          31 45 67;
          46 78 95;
          37 65 83;





          share|improve this answer


























          • It works. But could you explain the command. I didn't understand how it was done. Thank you.

            – rkatraga
            4 hours ago













          • @rkatraga I have added some explanation above

            – steeldriver
            4 hours ago











          • I understand the desired file name part. What I don't understand is how is awk splitting the file based on columns in each line. awk '{print>NF}' file.txt The above command is also doing the job (keeping aside the desired filename), I just don't get how it is splitting? The {print>NF} - What is happening here? I know NF is Number of Fields, but why is Splitting Happening.

            – rkatraga
            4 hours ago








          • 1





            @rkatraga It is more sorting than splitting. awk is processing the file line by line, as usual, and for each line it is executing a program that reads "print the current line to the file that is named after the current line's number of columns (fields)". Think about it as if you were sorting a deck of cards - reading the number printed on each one an putting it on the corresponding pile. (Sorry for the invasion, steeldriver).

            – fra-san
            3 hours ago








          • 1





            Got it. Great Thanks. Its just so beautiful. Can't appreciate enough.

            – rkatraga
            3 hours ago


















          3














          With awk, creating the desired filenames by concatenating fixed strings "file_" and"column.txt" with the internal variable NF (which contains the number of fields - or columns - in each input record - or line):



          awk '{print > "file_" NF "column.txt"}' file.txt


          Result:



          $ head file_?column.txt
          ==> file_1column.txt <==
          34;
          19;

          ==> file_2column.txt <==
          23 53;
          34 17;

          ==> file_3column.txt <==
          31 45 67;
          46 78 95;
          37 65 83;





          share|improve this answer


























          • It works. But could you explain the command. I didn't understand how it was done. Thank you.

            – rkatraga
            4 hours ago













          • @rkatraga I have added some explanation above

            – steeldriver
            4 hours ago











          • I understand the desired file name part. What I don't understand is how is awk splitting the file based on columns in each line. awk '{print>NF}' file.txt The above command is also doing the job (keeping aside the desired filename), I just don't get how it is splitting? The {print>NF} - What is happening here? I know NF is Number of Fields, but why is Splitting Happening.

            – rkatraga
            4 hours ago








          • 1





            @rkatraga It is more sorting than splitting. awk is processing the file line by line, as usual, and for each line it is executing a program that reads "print the current line to the file that is named after the current line's number of columns (fields)". Think about it as if you were sorting a deck of cards - reading the number printed on each one an putting it on the corresponding pile. (Sorry for the invasion, steeldriver).

            – fra-san
            3 hours ago








          • 1





            Got it. Great Thanks. Its just so beautiful. Can't appreciate enough.

            – rkatraga
            3 hours ago
















          3












          3








          3







          With awk, creating the desired filenames by concatenating fixed strings "file_" and"column.txt" with the internal variable NF (which contains the number of fields - or columns - in each input record - or line):



          awk '{print > "file_" NF "column.txt"}' file.txt


          Result:



          $ head file_?column.txt
          ==> file_1column.txt <==
          34;
          19;

          ==> file_2column.txt <==
          23 53;
          34 17;

          ==> file_3column.txt <==
          31 45 67;
          46 78 95;
          37 65 83;





          share|improve this answer















          With awk, creating the desired filenames by concatenating fixed strings "file_" and"column.txt" with the internal variable NF (which contains the number of fields - or columns - in each input record - or line):



          awk '{print > "file_" NF "column.txt"}' file.txt


          Result:



          $ head file_?column.txt
          ==> file_1column.txt <==
          34;
          19;

          ==> file_2column.txt <==
          23 53;
          34 17;

          ==> file_3column.txt <==
          31 45 67;
          46 78 95;
          37 65 83;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 4 hours ago

























          answered 5 hours ago









          steeldriversteeldriver

          37.9k45489




          37.9k45489













          • It works. But could you explain the command. I didn't understand how it was done. Thank you.

            – rkatraga
            4 hours ago













          • @rkatraga I have added some explanation above

            – steeldriver
            4 hours ago











          • I understand the desired file name part. What I don't understand is how is awk splitting the file based on columns in each line. awk '{print>NF}' file.txt The above command is also doing the job (keeping aside the desired filename), I just don't get how it is splitting? The {print>NF} - What is happening here? I know NF is Number of Fields, but why is Splitting Happening.

            – rkatraga
            4 hours ago








          • 1





            @rkatraga It is more sorting than splitting. awk is processing the file line by line, as usual, and for each line it is executing a program that reads "print the current line to the file that is named after the current line's number of columns (fields)". Think about it as if you were sorting a deck of cards - reading the number printed on each one an putting it on the corresponding pile. (Sorry for the invasion, steeldriver).

            – fra-san
            3 hours ago








          • 1





            Got it. Great Thanks. Its just so beautiful. Can't appreciate enough.

            – rkatraga
            3 hours ago





















          • It works. But could you explain the command. I didn't understand how it was done. Thank you.

            – rkatraga
            4 hours ago













          • @rkatraga I have added some explanation above

            – steeldriver
            4 hours ago











          • I understand the desired file name part. What I don't understand is how is awk splitting the file based on columns in each line. awk '{print>NF}' file.txt The above command is also doing the job (keeping aside the desired filename), I just don't get how it is splitting? The {print>NF} - What is happening here? I know NF is Number of Fields, but why is Splitting Happening.

            – rkatraga
            4 hours ago








          • 1





            @rkatraga It is more sorting than splitting. awk is processing the file line by line, as usual, and for each line it is executing a program that reads "print the current line to the file that is named after the current line's number of columns (fields)". Think about it as if you were sorting a deck of cards - reading the number printed on each one an putting it on the corresponding pile. (Sorry for the invasion, steeldriver).

            – fra-san
            3 hours ago








          • 1





            Got it. Great Thanks. Its just so beautiful. Can't appreciate enough.

            – rkatraga
            3 hours ago



















          It works. But could you explain the command. I didn't understand how it was done. Thank you.

          – rkatraga
          4 hours ago







          It works. But could you explain the command. I didn't understand how it was done. Thank you.

          – rkatraga
          4 hours ago















          @rkatraga I have added some explanation above

          – steeldriver
          4 hours ago





          @rkatraga I have added some explanation above

          – steeldriver
          4 hours ago













          I understand the desired file name part. What I don't understand is how is awk splitting the file based on columns in each line. awk '{print>NF}' file.txt The above command is also doing the job (keeping aside the desired filename), I just don't get how it is splitting? The {print>NF} - What is happening here? I know NF is Number of Fields, but why is Splitting Happening.

          – rkatraga
          4 hours ago







          I understand the desired file name part. What I don't understand is how is awk splitting the file based on columns in each line. awk '{print>NF}' file.txt The above command is also doing the job (keeping aside the desired filename), I just don't get how it is splitting? The {print>NF} - What is happening here? I know NF is Number of Fields, but why is Splitting Happening.

          – rkatraga
          4 hours ago






          1




          1





          @rkatraga It is more sorting than splitting. awk is processing the file line by line, as usual, and for each line it is executing a program that reads "print the current line to the file that is named after the current line's number of columns (fields)". Think about it as if you were sorting a deck of cards - reading the number printed on each one an putting it on the corresponding pile. (Sorry for the invasion, steeldriver).

          – fra-san
          3 hours ago







          @rkatraga It is more sorting than splitting. awk is processing the file line by line, as usual, and for each line it is executing a program that reads "print the current line to the file that is named after the current line's number of columns (fields)". Think about it as if you were sorting a deck of cards - reading the number printed on each one an putting it on the corresponding pile. (Sorry for the invasion, steeldriver).

          – fra-san
          3 hours ago






          1




          1





          Got it. Great Thanks. Its just so beautiful. Can't appreciate enough.

          – rkatraga
          3 hours ago







          Got it. Great Thanks. Its just so beautiful. Can't appreciate enough.

          – rkatraga
          3 hours ago












          rkatraga is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          rkatraga is a new contributor. Be nice, and check out our Code of Conduct.













          rkatraga is a new contributor. Be nice, and check out our Code of Conduct.












          rkatraga is a new contributor. Be nice, and check out our Code of Conduct.
















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512848%2fhow-to-split-file-based-on-number-of-columns%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

          Hudson River Historic District Contents Geography History The district today Aesthetics Cultural...

          The number designs the writing. Feandra Aversely Definition: The act of ingrafting a sprig or shoot of one...

          Ayherre Geografie Demografie Externe links Navigatiemenu43° 23′ NB, 1° 15′ WL43° 23′ NB, 1°...