按行拆分文件,删除第一个字段并将其用作结果片段的名称

按行拆分文件,删除第一个字段并将其用作结果片段的名称

我有这样一个文本文件:

19-198-0000 NORTHANGER ABBEY
19-198-0001 THIS LITTLE WORK WAS FINISHED IN THE YEAR EIGHTEEN O THREE AND INTENDED FOR IMMEDIATE PUBLICATION IT WAS DISPOSED OF TO A BOOKSELLER IT WAS EVEN ADVERTISED
19-198-0002 NEITHER THE AUTHOR NOR THE PUBLIC HAVE ANY OTHER CONCERN THAN AS SOME OBSERVATION IS NECESSARY UPON THOSE PARTS OF THE WORK WHICH THIRTEEN YEARS HAVE MADE COMPARATIVELY OBSOLETE
19-198-0003 THE PUBLIC ARE ENTREATED TO BEAR IN MIND THAT THIRTEEN YEARS HAVE

等等。我想要的是一种创建与初始文件中的行相同数量的较小文本文件的方法,它们的名称是行开头的数字,它们的内容是其余的。

例如,从上面的小片段:

19-198-0000.txt  would contain: NORTHANGER ABBEY
19-198-0001.txt  would contain: THIS LITTLE WORK WAS FINISHED IN THE YEAR EIGHTEEN O THREE AND INTENDED FOR IMMEDIATE PUBLICATION IT WAS DISPOSED OF TO A BOOKSELLER IT WAS EVEN ADVERTISED
19-198-0002.txt  would contain: NEITHER THE AUTHOR NOR THE PUBLIC HAVE ANY OTHER CONCERN THAN AS SOME OBSERVATION IS NECESSARY UPON THOSE PARTS OF THE WORK WHICH THIRTEEN YEARS HAVE MADE COMPARATIVELY OBSOLETE
19-198-0003.txt  would contain: THE PUBLIC ARE ENTREATED TO BEAR IN MIND THAT THIRTEEN YEARS HAVE

等等。

答案1

while read -r tag line; do
    printf '%s\n' "$line" >"$tag".txt
done <text.in

当您使用 读取两个变量时read,第一个单词将放入第一个变量中,该行的其余部分将放入第二个变量中。

“单词”的构成取决于 的值$IFS。默认情况下,read将根据空格、制表符或换行符将行拆分为单词。

循环使用它将该行(减去第一个单词或“标记”)输出到一个文件,该文件的名称取自该行的开头。

答案2

bash根本不需要:

awk '{file = $1".txt"; $1=""; print substr($0,2) > file; close(file)}' a_text_file
  • file从第一列中获取,.txt在其后面附加 a。
  • 消除第一列。
  • 将剩余的列(减去其他前导空格)打印到file.

同时,关于该while循环bash

$ (echo first; echo -n second) | while read line; do echo $line; done
first
$ 

嗯,无声数据丢失。最好确保有总是最终换行符,是吗?

答案3

设法做到了:

#!/bin/bash

while read -r filename content ; do
    printf '%s\n' "$content" >> "${filename}.txt"
done < some_file.txt

相关内容