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;
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
New contributor
add a comment |
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
New contributor
add a comment |
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
New contributor
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
text-processing awk
New contributor
New contributor
edited 11 hours ago
αғsнιη
17.2k103069
17.2k103069
New contributor
asked 11 hours ago
rkatragarkatraga
175
175
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
add a comment |
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;
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
add a comment |
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;
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;
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
add a comment |
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
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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