将文件中的文件名和信息保存到一个两列的 txt 文档中。 ubuntu 终端
save filename and information from the file into a two column txt doc. ubuntu terminal
我有一个关于在 ubuntu 终端中操作和创建文本文件的问题。我有一个包含几个 1000 个子目录的目录。在每个目录中,都有一个扩展名为 stats.txt 的文件。我想写一段代码,从父目录 运行 ,并创建一个文件,第一列中的所有 stats.txt 文件的名称,然后 returns 给我下一列中同一 stats.txt 文件第 5 行的所有信息。 stats.txt 文件的第 5 行是六个单词的句子,而不是单个值。
作为参考,我已经成功地将 sed 命令与 find 和 cat 结合使用,生成了一个包含每个 stats.txt 文件的第 5 行的文件。然后我使用 ls 命令保存我所有子目录的列表。我假设这两个文件都按子目录的字母顺序排列,因此很容易合并,但我错了。 find 和 cat 函数,或者至少是我对它们的实现,生成了一个随机顺序的文件(见下文)。无需尝试修复此代码,我对所有解决方案都持开放态度。
# loop through subdirectories and save the 5th line of stats.txt as a different file.
for f in ~/*; do [ -d $f ] && cd "$f" && sed -n 5p *stats.txt > final.stats.txt done;
# find the final.stats.txt files and save them as a single file
find ./ -name 'final.stats.txt' -exec cat {} \; > compiled.stats.txt
也许这样的事情可以帮助你走上正轨:
find . -name "*stats.txt" -exec awk 'FNR==5{print FILENAME, [=10=]}' '{}' + > compiled.stats
我有一个关于在 ubuntu 终端中操作和创建文本文件的问题。我有一个包含几个 1000 个子目录的目录。在每个目录中,都有一个扩展名为 stats.txt 的文件。我想写一段代码,从父目录 运行 ,并创建一个文件,第一列中的所有 stats.txt 文件的名称,然后 returns 给我下一列中同一 stats.txt 文件第 5 行的所有信息。 stats.txt 文件的第 5 行是六个单词的句子,而不是单个值。
作为参考,我已经成功地将 sed 命令与 find 和 cat 结合使用,生成了一个包含每个 stats.txt 文件的第 5 行的文件。然后我使用 ls 命令保存我所有子目录的列表。我假设这两个文件都按子目录的字母顺序排列,因此很容易合并,但我错了。 find 和 cat 函数,或者至少是我对它们的实现,生成了一个随机顺序的文件(见下文)。无需尝试修复此代码,我对所有解决方案都持开放态度。
# loop through subdirectories and save the 5th line of stats.txt as a different file.
for f in ~/*; do [ -d $f ] && cd "$f" && sed -n 5p *stats.txt > final.stats.txt done;
# find the final.stats.txt files and save them as a single file
find ./ -name 'final.stats.txt' -exec cat {} \; > compiled.stats.txt
也许这样的事情可以帮助你走上正轨:
find . -name "*stats.txt" -exec awk 'FNR==5{print FILENAME, [=10=]}' '{}' + > compiled.stats