Skip to content
Advertisement

batch extracting data from files, naming new files according to string in input file

With Linux I want to automatically extract data from .dat files and name the new files according to a string in the input files:

I have 300 .dat files with a data structure as follows:

.
.
.
DE name1, contig1 .
.
SQ
information1
//
.
.
DE name1, contig2 .
.
SQ
information2
//
.

where the “.” stands for lines that I don’t need. I now want to extract all the “information” from the .dat file and generate a new file with the name “name1” from the line DE.

JavaScript

What command would you recommend to perform this task ?

Advertisement

Answer

You can use this awk 1 liner:

awk -F '[, ]' '/^DE/ {filename=$2} /SQ/,//// {print > filename}' file.dat

And here is a sample run:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement