合并两个 bash 历史文件

Mik*_*eek 5 unix linux bash sed

假设我有两个 bash 历史文件,如下所示:

历史1.txt

    1  ls
    2  cd foo
...
  921  history > history1.txt
Run Code Online (Sandbox Code Playgroud)

历史2.txt

  154  vim /etc/nginx/nginx.conf
  155  service nginx restart
...
 1153  history > history2.txt
Run Code Online (Sandbox Code Playgroud)

我知道我可以轻松编写一个 bash 脚本来将这两个文件合并在一起,以便生成的文件包含第 1 行到 1153 行,而没有重复的历史记录条目...就像以下 bash 脚本一样:

合并.sh

HEAD=`head -n 1 history2.txt | sed -e 's/^[[:space:]]*//'`
sed -n -e "1,/$HEAD/p" history1.txt > merged.txt
sed -e "1,$ s/$HEAD//" -e '/^\s*$/d' history2.txt >> merged.txt
Run Code Online (Sandbox Code Playgroud)

但我花了比我愿意承认的更多的时间来尝试找到一种仅使用未命名管道、sed 和/或任何其他常见 Linux 实用程序而不使用变量和命令替换(“command”)来完成此操作的方法。我没有取得任何成功:(

任何 Linux shell 专家或 sed 大师都知道这是否可能吗?

注意:我知道 merge.sh 脚本不适用于所有边缘情况。

rus*_*ush 3

如果我理解正确,那么您只有 2 个没有重复条目的文件(尽管不确定)。

所以你不需要sed这里的东西。只是:

cat history1.txt history2.txt | sort -n > output
Run Code Online (Sandbox Code Playgroud)

如果您有重复项:

cat history1.txt history2.txt | sort -n -u > output
Run Code Online (Sandbox Code Playgroud)