How to print lines that have values in certain columns ?

Hi, everyone
I have a dataset like this:

A B C D 
A C 
  C D E
F G H
  F D K
  Y
  X A
K K C G

some of columns have no values in each line. I want to print all lines that have 1/2/3/4 values, export separately to four files. What I expected is like this:
file1

Y

file 2

A C
X A

file 3

C D E
F D K

file 4

A B C D
K K C G

It's not necessarily to merge the command, I can do it individually if the pattern is the same.
Thanks very much.

$ 
$ cat dataset_1.txt
A B C D 
A C 
  C D E
F G H
  F D K
  Y
  X A
K K C G
$ 
$ awk '{$1=$1; print>"file"NF}' dataset_1.txt
$ 
$ cat file1
Y
$ 
$ cat file2
A C
X A
$ 
$ cat file3
C D E
F G H
F D K
$ 
$ cat file4
A B C D
K K C G
$ 
$ 
1 Like

wow, that's great, very appreciated. :slight_smile:

That will work with some versions of awk and give you a syntax error with other versions of awk . The following should work with almost any version of awk :

awk '{$1=$1; print>("file"NF)}' dataset_1.txt
3 Likes