问题
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:
- Is AWK the appropriate program for this task? Or perhaps there are more convinient ways doing this?
- How would this task be done?
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/21555588/using-the-first-field-in-awk-as-file-name