次のテキストファイルがあります。
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
2 つの変数読み取りを使用すると、read
最初の単語が最初の変数に配置され、残りの行が 2 番目の変数に配置されます。
「単語」の構成は の値によって異なります$IFS
。デフォルトでは、read
行はスペース、タブ、または改行に基づいて単語に分割されます。
ループで使用すると、行の先頭から名前を取得するファイルに行(最初の単語または「タグ」を除く)が出力されます。