Using the first field in AWK as file name

冷暖自知 提交于 2019-12-21 17:01:17

问题


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?

回答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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!