Question

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:

  1. Is AWK the appropriate program for this task? Or perhaps there are more convinient ways doing this?
  2. How would this task be done?
Was it helpful?

Solution

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

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top