我有一个如下所示的文本文件:
6789 Here is a bunch of text
4565 More text......................................................
.....etc
我需要提取第一个单词(数字)并创建一个包含以下所有文本的新文件,将第一个单词作为文件名直到一个空行,然后重复创建许多文本文件。
最佳答案
awk 行可以做到:
awk 'BEGIN{RS=""} {fn=$1;gsub(/^[0-9]+\s/,"");print $0 > (fn".txt")}' yourFile
测试:
kent$ l
total 0
(no file in current dir)
kent$ echo "6789 Here is a bunch of text
4565 More text......................................................
.....etc"|awk 'BEGIN{RS=""} {fn=$1;gsub(/^[0-9]+\s/,"");print $0 > (fn".txt")}'
kent$ ls
4565.txt 6789.txt
and, the contents are:
kent$ head *.txt
==> 4565.txt <==
More text......................................................
.....etc
==> 6789.txt <==
Here is a bunch of text
关于regex - 基于空行的新文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8056519/