2

I have a csv file on a Linux machine like this:

value1 value2 name  
1      2      a  
2      3      b  
4      5      c  

What I want is to split the n entries in this csv into n text files with just the corresponding numbers in that file separated by space. N is determined by the value in name.

So above csv would become:

  • a.txt containing 1 2
  • b.txt containing 2 3
  • c.txt containing 4 5

In case there are multiple entries for a particular name then those should go together to one file.

Ex if there was

1  2  a   
21 31 a

then there should be a single a.txt with

1  2     
21 31
3

It's pretty simple actually

awk '{print $1, $2 > $3 ".txt"}' file.csv

You should only run into to trouble if there will be more than about one thousand new files

  • 1
    Why will >1k files be a problem? I just tested it with perl -le 'for(0..10000){print "a$_ b$_ $_"}' > file; awk '{print $1, $2 > $3 ".txt"}' file and that created 10001 files with no complaint. – terdon Jun 1 '18 at 16:14
  • do $1 $2 $3 refer to the columns? I have only 70 files as of now. – kRazzy R Jun 1 '18 at 16:23
  • In case they are not space separated but comma separated values in the csv , how do I address it in the awk script? thank you in advance. – kRazzy R Jun 1 '18 at 16:34
  • 1
    @terdon, what is your ulimit -n? awk should not be able to open more files than that. – glenn jackman Jun 1 '18 at 17:09
  • 1
    @kRazzyR, then you tell awk what the input and output field separators are: awk 'BEGIN {FS = ","; OFS = ","} {print $1,$2 > $3".txt"}' file -- or more tersely awk -F, '{print $1,$2 > $3".txt"}' OFS=, file – glenn jackman Jun 1 '18 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.