Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the first field in AWK as file name

Tags:

bash

awk

corpus

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?
like image 446
beyeran Avatar asked Feb 04 '14 14:02

beyeran


2 Answers

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
like image 195
fedorqui 'SO stop harming' Avatar answered Sep 25 '22 15:09

fedorqui 'SO stop harming'


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
like image 39
glenn jackman Avatar answered Sep 23 '22 15:09

glenn jackman