The dataset is one big file with three columns: An ID of a section, something irrelevant and a line of text. An example could look like the following:
A01 001 This is a simple test.
A01 002 Just for exemplary purpose.
A01 003
A02 001 This is another text
I want to use the first column (in this example A01 and A02, which represent different texts) to be the file name, whichs content is everything in that line after the second column.
The example above should result two files, one with name A01
and content:
This is a simple test.
Just for exemplary purpose.
and another one A02
with content:
This is another text
My questions are:
awk
is perfect for these kind of tasks. If you do not mind to have some leading spaces, you can use:
awk '{f=$1; $1=$2=""; print > f}' file
This will empty first and second fields and then print all the line into the f
file, which was previously stored as first field.
And in case these spaces are bothering, you can delete them with sub(" ", "")
:
awk '{f=$1; $1=$2=""; sub(" ", ""); print > f}' file
Bash will work too. Probably slower than awk if that's a concern
while read -r id num line; do
[[ $line ]] && echo "$line" >> $id
done < file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With