3

I have a file that contains four columns and 5000 rows. I want to make 5000 new files from this file so that each file has one row from the original file. Also, I want to name the new files according to the values in the 4th column. Example: The following file (XXXX.txt) has four rows

File: XXXX.txt

1 315 4567 G1
1 212 345 G2
2 315 25674 G3
3 12 235673 G4

Expected New Files

File: G1

1 315 4567 G1

File: G2

1 212 345 G2

File: G3

2 315 25674 G3

File: G4

3 12 235673 G4

I have tried this command:

awk '{print > $0}' < XXXX.txt

This command makes new files as desired, but I am not able to name new files as per column4 of the original file.

0

3 Answers 3

2

You can try by slightly change the awk script:

awk '{print > $4}' XXXX.txt

But be aware if in the source file there is row with same 4th column as other the end file will contain only the last row. You can try something like to avoid it:

awk '{print >> $4}' XXXX.txt

N.B. Do not run it more than once as this will add the records twice.

If you eventually get "too many open files" error you can use script like this to explicitly close the output file(s)

awk '{print >> $4;close($4)}' XXXX.txt
11
  • 2
    You may get a redirection error if null string is passed. A workaround could be awk '/./{print > $NF}' < XXXX.txt
    – HatLess
    May 21 at 15:24
  • 3
    @HatLess, there are many possible "may". Just let give OP a place for discussion :) May 21 at 15:25
  • 3
    GNU awk leaves the file open: gnu.org/software/gawk/manual/html_node/Redirection.html. My mawk man page says "Redirection opens file or command only once, subsequent redirections append to the already open stream." May 21 at 16:56
  • 1
    Also the > redirection does not act like the shell's: it will append to the file after truncating it the first time (assuming the file does not need to be closed) May 21 at 16:57
  • 2
    to append in awk i will need not to close the files. - yes, or use >> instead of >. Regarding if I do not close them will get evidentially "too many open file" - yes, unless you're using GNU awk and then it'll just slow down as it tries to internally manage all of the virtually open files.
    – Ed Morton
    May 22 at 20:21
2

You are somewhat close. awk '{print > $0}' is equivalent to awk '{print $0 > $0}', which is easier to understand.

If you print a line to file called by your desired 4'th column ($4) instead of whole line ($0) then it's going to be correct.

awk '{print $0 > $4}'
2
-3
while read line
do 
finame=$(echo $line| awk '{print $NF}')
echo $line >$finame
done<filer

output

cat G1
1 315 4567 G1

cat G2
1 212 345 G2

cat G3
2 315 25674 G3

cat G4
3 12 235673 G4
1
  • There are already far better AWK approaches in other answers. May 24 at 10:50

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.