0

I have a file (file.csv) which looks like this:

abc 123
abc 001
abc 014
bos 011
bos 034
bos 079
bos 004
...

PS: column 1 of file.csv has 27297 unique words in total, and file.csv has 789234 rows in total

I want to split it into multiple files basing on the first column. and I use awk to deal with it:

awk '{x=++i}{print $2 > "HG"x".txt"}' file.csv

however, the name of the output was HG1.txt, HG2.txt,..., HG27297.txt. what I want is that the file name of output should be formated, they should look like HG000001.txt, HG000002.txt,..., HG027297.txt.

1 Answer 1

2

{x=++i} would result in 2 variables, x and i, which both have the same value as the builtin variable NR (and FNR).

print $2 > "HG"x".txt" is undefined behavior due to no parens around the expression on the right side of >.

Not closing output files as you go would result in a "too many open files" error once you get past a threshold when using all but GNU awk.

Assuming your input is grouped by the first field values as shown in your example (if not, run sort on the input first):

awk '$1!=prev{close(out); out=sprintf("HG%06d.txt",++c); prev=$1} {print > out}' file.csv

Obviously change print > out to print $2 > out if you truly only want the 2nd column printed as in your code.

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.