justaguy
justaguy

Reputation: 3022

awk to combine all lines in file with another

The below awk combines the target.txt with the out_parse.txt and the output is GJ-53.txt. If there are multiple lines in out_parse how can they both be written to GJ-53.txt? As of now the first line of out_parse saves to a text file GJ-53, but the second line does not. Than you :).

awk '{close(fname)} (getline fname<f)>0 {print>fname}' f=target.txt out_parse.txt

Contents of out_parse.txt

13  20763612    20763612    C   T
13  20763620    20763620    A   G

Contents of target.txt

GJ-53.txt

 cat -v out_parse.txt
 13      20763612        20763612        C       T
 13      20763620        20763620        A       G

Upvotes: 0

Views: 73

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74596

If I understand correctly, you want to copy the contents of out_parse.txt to a new file, whose name is given in the file target.txt. To do that, you don't really need to use awk at all:

cp out_parse.txt "$(< target.txt)"

In bash, $(< file) can be use as a substitution for the contents of file. It achieves the same thing as $(cat file).

If you wanted to use awk, you could do something like this:

awk 'NR==FNR{f=$0;next}{print>f}' target.txt out_parse.txt

The first block applies to the first file, where the total record number NR is equal to the current file's record number FNR. It saves the content of the line (i.e. the filename) to f and skips any further instructions. The second block applies only to the second file and prints every line to the filename saved in f.

Upvotes: 2

Related Questions