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;

2 Answers 2

4

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;
5
  • It works. But could you explain the command. I didn't understand how it was done. Thank you.
    – rkatraga
    Commented Apr 16, 2019 at 19:17
  • @rkatraga I have added some explanation above Commented Apr 16, 2019 at 19:49
  • 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
    Commented Apr 16, 2019 at 19:59
  • 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
    Commented Apr 16, 2019 at 20:42
  • 1
    Got it. Great Thanks. Its just so beautiful. Can't appreciate enough.
    – rkatraga
    Commented Apr 16, 2019 at 20:48
0

we can with below command too

command

split -l 3 file.txt

it will split into 3 files with xaa,xab,xcc now we renaming xaa==: file_xaa and so on

ls -ltr xa*| awk '{print "mv" " " $NF " " "file_"$NF}'| sh


n@praveen:~$ cat file_xaa

23 53;
34;
praveen@praveen:~$ cat file_xab 
31 45 67;
46 78 95;
34 17;
praveen@praveen:~$ cat file_xac
19;
37 65 83;
2
  • The task is to split the lines of the file into separate files based on the number of columns in each line.
    – Kusalananda
    Commented Apr 25, 2019 at 21:05
  • Sorry for confused Commented Apr 25, 2019 at 21:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .