如何将多个文本文件组合成一个按创建日期排序的文本文件?

我是新手所以请帮助:

我使用Scratch在我的iPhone上保存日记,将我所做的所有笔记输出到存储在Dropbox中的单独的.txt文件中。

我已经与我的Ubuntu 14.04系统同步,所以在我的文件中我有一个文件夹,其中存储了所有文本文件:

/家庭/斯图尔特/ Dropbox的/划痕

我想运行一个命令,将所有这些文件连接成一个文件,并附带以下条款:

  1. 按创建日期排序(最早的文件优先)
  2. 在文件内容之前的单独行上打印文件的日期
  3. 在每个文件后面包含一个空行,后跟某种分隔线

所以输出文件的条目看起来像这样:

12-01-2014 11:01 AM:
在以色列的一家咖啡店。 外面的标志写着:
“咖啡” – 9谢克尔
“请喝咖啡” – 8谢克尔
“早上好,我可以请一杯咖啡吗?” – 7谢克尔


25-01-2014 11:01 AM:
你不能超越你的自我 – Gunnar solskjaer

等等。 我曾经使用过这种自动追加的不同应用,但我不知道如何复制它。

我已经查看了很多这里的帮助文件,但是我找不到任何可以帮助我想到的输出。

任何帮助非常感谢!


更多信息

我尝试创建下面建议的脚本并按照步骤操作。 但是我收到了这样的答复:

stuart @ StudioClough:/ home $ chmod + x $ HOME / my_concat

stuart @ StudioClough:/ home $ ./my_concat / home / stuart / Dropbox / Scratch> new_concatenated_file

bash:new_concatenated_file:权限被拒绝

我必须以某种方式运行它作为sudo?

它可以用python脚本完成,只有一个注释:我采用了修改日期而不是创建日期 ,因为创建日期几乎肯定与实际创建日期不匹配:它是文件复制到计算机的日期,虽然修改期间修改日期似乎没有变化(请参阅@cOrps答案中的讨论)。 你必须看看它是否适用于你的情况。

如果您可以接受,则可以使用下面的脚本创建包含注释的组合文件。 它读取注释,对它们进行排序并将它们附加到文本文件中(如果它不存在则创建它)。

好消息是,您可以将新笔记附加到同一个文件而不会覆盖旧笔记。

输出示例:

 Mon Sep 29 08:48:31 2014 This is my first note. As you can read, I am not really awake yet. ---------- Mon Sep 29 09:04:06 2014 It is really time I am going to eat something. I am a bit hungry. Making it a bit longer. ---------- 

如何使用:

  • 将下面的脚本复制到一个空文件中,将其另存为add_notes.py
  • 更改files_dir (您的注释所在的位置)和要保存注释的文件的目录: combined_file (如果文件不存在,脚本将创建该文件)
  • 通过键入以下命令在终端窗口中运行脚本:

     python3 /path/to/add_notes.py 

剧本:

 #!/usr/bin/env python3 import os import time import subprocess # -------------------------------------------------------- files_dir = "/path/to/your/textfiles" combined_file = "/path/to/your/combined/file.txt" # --------------------------------------------------------- notes = [] if not os.path.exists(combined_file): subprocess.Popen(["touch", combined_file]) def read_file(file): with open(file) as note: return note.read() def append_file(combined_file, text): with open(combined_file, "a") as notes: notes.write(text) for root, dirs, files in os.walk(files_dir): for name in files: subject = root+"/"+name cr_date_text = time.ctime(os.path.getmtime(subject)) cr_date_n = os.stat(subject).st_mtime notes.append((cr_date_n, cr_date_text, subject)) notes.sort(key=lambda x: x[0]) for note in notes: text = note[1]+"\n"+read_file(note[2])+"\n"+"-"*10+"\n" append_file(combined_file, text) 

这是一个bash解决方案。 如果您使用ext4文件系统,它应该工作。 它使用ext4存储在crtime字段中的文件创建日期。

随处创建此脚本。 让我们说$HOME目录中的my_concat (在你的情况下是/home/stuart ):

 #!/bin/bash get_crtime() { for target in "${@}"; do inode=$(ls -di "${target}" | cut -d ' ' -f 1) fs=$(df --output=source "${target}" | tail -1) crtime=$(sudo debugfs -R 'stat <'"${inode}"'>' "${fs}" 2>/dev/null | grep -oP 'crtime.*--\s*\K.*') printf "%s\n" "${crtime}" done } get_epoch_crtime(){ date --date "$(get_crtime $1)" +%s } get_epoch_mtime() { stat -c %Y $1 } # takes two date as input, returns earlier date get_earlier_time(){ if [[ "$1" -lt "$2" ]]; then echo $(date -d @$1 +%m/%d/%Y:%H:%M:%S) else echo $(date -d @$2 +%m/%d/%Y:%H:%M:%S) fi } if [ $# != 1 ]; then echo "Required only one argument - full path to folder" echo "Usage example:" echo "$0 /var/log/syslog/" exit 1 fi if [ -d "$1" ]; then cd $1 for file in * do echo $(get_earlier_time $(get_epoch_crtime $file) $(get_epoch_mtime $file)) cat $file echo -e "\n-------" done else echo "The folder specified is not exists ($1). Please enter full path" fi 

使其可执行:

 chmod +x $HOME/my_concat 

现在转到$HOME文件夹并运行脚本。 脚本将询问您的密码,因为脚本使用sudo

 ./my_concat /home/stuart/Dropbox/Scratch > new_concatenated_file 

现在使用一些编辑器读取new_concatenated_file

 gedit new_concatenated_file 

此脚本使用创建日期修改日期 ,比较后需要最早的一个。

来源

  1. 关于创建日期
  2. 在其他文件系统中创建日期
  3. 用于查找创建日期的脚本