Using the first field in AWK as file name

对着背影说爱祢 提交于 2019-12-04 08:47:44

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