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?

List *all* the tuples!

How come Sam didn't become Lord of Horn Hill?

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

How to align text above triangle figure

List of Python versions

Why didn't this character "real die" when they blew their stack out in Altered Carbon?

Do I really need recursive chmod to restrict access to a folder?

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

What is the logic behind the Maharil's explanation of why we don't say שעשה ניסים on Pesach?

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

Book where humans were engineered with genes from animal species to survive hostile planets

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

What does F' and F" mean?

What exactly is a "Meth" in Altered Carbon?

porting install scripts : can rpm replace apt?

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

How to find out what spells would be useless to a blind NPC spellcaster?

What is Arya's weapon design?

Okay to merge included columns on otherwise identical indexes?

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

Understanding Ceva's Theorem

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

How to tell that you are a giant?

What is Wonderstone and are there any references to it pre-1982?



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 11 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 11 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
            11 hours ago












          • @rkatraga I have added some explanation above

            – steeldriver
            10 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
            10 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
            9 hours ago







          • 1





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

            – rkatraga
            9 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
            11 hours ago












          • @rkatraga I have added some explanation above

            – steeldriver
            10 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
            10 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
            9 hours ago







          • 1





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

            – rkatraga
            9 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
            11 hours ago












          • @rkatraga I have added some explanation above

            – steeldriver
            10 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
            10 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
            9 hours ago







          • 1





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

            – rkatraga
            9 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 10 hours ago

























          answered 11 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
            11 hours ago












          • @rkatraga I have added some explanation above

            – steeldriver
            10 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
            10 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
            9 hours ago







          • 1





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

            – rkatraga
            9 hours ago


















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

            – rkatraga
            11 hours ago












          • @rkatraga I have added some explanation above

            – steeldriver
            10 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
            10 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
            9 hours ago







          • 1





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

            – rkatraga
            9 hours ago

















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

          – rkatraga
          11 hours ago






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

          – rkatraga
          11 hours ago














          @rkatraga I have added some explanation above

          – steeldriver
          10 hours ago





          @rkatraga I have added some explanation above

          – steeldriver
          10 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
          10 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
          10 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
          9 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
          9 hours ago





          1




          1





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

          – rkatraga
          9 hours ago






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

          – rkatraga
          9 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







          -awk, text-processing

          Popular posts from this blog

          Mobil Contents History Mobil brands Former Mobil brands Lukoil transaction Mobil UK Mobil Australia Mobil New Zealand Mobil Greece Mobil in Japan Mobil in Canada Mobil Egypt See also References External links Navigation menuwww.mobil.com"Mobil Corporation"the original"Our Houston campus""Business & Finance: Socony-Vacuum Corp.""Popular Mechanics""Lubrite Technologies""Exxon Mobil campus 'clearly happening'""Toledo Blade - Google News Archive Search""The Lion and the Moose - How 2 Executives Pulled off the Biggest Merger Ever""ExxonMobil Press Release""Lubricants""Archived copy"the original"Mobil 1™ and Mobil Super™ motor oil and synthetic motor oil - Mobil™ Motor Oils""Mobil Delvac""Mobil Industrial website""The State of Competition in Gasoline Marketing: The Effects of Refiner Operations at Retail""Mobil Travel Guide to become Forbes Travel Guide""Hotel Rankings: Forbes Merges with Mobil"the original"Jamieson oil industry history""Mobil news""Caltex pumps for control""Watchdog blocks Caltex bid""Exxon Mobil sells service station network""Mobil Oil New Zealand Limited is New Zealand's oldest oil company, with predecessor companies having first established a presence in the country in 1896""ExxonMobil subsidiaries have a business history in New Zealand stretching back more than 120 years. We are involved in petroleum refining and distribution and the marketing of fuels, lubricants and chemical products""Archived copy"the original"Exxon Mobil to Sell Its Japanese Arm for $3.9 Billion""Gas station merger will end Esso and Mobil's long run in Japan""Esso moves to affiliate itself with PC Optimum, no longer Aeroplan, in loyalty point switch""Mobil brand of gas stations to launch in Canada after deal for 213 Loblaws-owned locations""Mobil Nears Completion of Rebranding 200 Loblaw Gas Stations""Learn about ExxonMobil's operations in Egypt""Petrol and Diesel Service Stations in Egypt - Mobil"Official websiteExxon Mobil corporate websiteMobil Industrial official websiteeeeeeeeDA04275022275790-40000 0001 0860 5061n82045453134887257134887257

          Frič See also Navigation menuinternal link

          Identify plant with long narrow paired leaves and reddish stems Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?What is this plant with long sharp leaves? Is it a weed?What is this 3ft high, stalky plant, with mid sized narrow leaves?What is this young shrub with opposite ovate, crenate leaves and reddish stems?What is this plant with large broad serrated leaves?Identify this upright branching weed with long leaves and reddish stemsPlease help me identify this bulbous plant with long, broad leaves and white flowersWhat is this small annual with narrow gray/green leaves and rust colored daisy-type flowers?What is this chilli plant?Does anyone know what type of chilli plant this is?Help identify this plant