计数bash中两个文件之间的字符差

发布于 2025-01-24 23:31:55 字数 190 浏览 5 评论 0原文

计算两个文件中字符串之间不同字符数量的最佳方法。我知道如何在Python中执行此操作,但我需要一个bash解决方案。

文件内容:

ABCDEF
ABDCEF

输出:

2

谢谢

What would be the best way to count the number of characters that are different between strings in two files. I know how to do this in Python but I need a bash solution.

File contents:

ABCDEF
ABDCEF

Output:

2

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2025-01-31 23:31:55

将行计数

使用cmp将文件与-L的详细说明进行比较,然后用wccmp -bl -bl file1 file2 | 。 wc -l <​​/code>

http://linux.die.net/man/1/cmp

Use cmp to compare files with -l for verbose, then count the lines with wc:

cmp -bl file1 file2 | wc -l

http://linux.die.net/man/1/cmp

深居我梦 2025-01-31 23:31:55

以下有效,

#!/bin/bash
read -p "Filename: " file1
    awk '(NR>1)' $file1 | tee file1.tmp
        for file in *.txt
            do awk '(NR>1)' $file > $file2.tmp
            cmp -bl file1.tmp $file2.tmp | wc -l
            rm $file2.tmp
        done

function finish {
    rm file1.tmp
}
trap finish EXIT

The following worked,

#!/bin/bash
read -p "Filename: " file1
    awk '(NR>1)' $file1 | tee file1.tmp
        for file in *.txt
            do awk '(NR>1)' $file > $file2.tmp
            cmp -bl file1.tmp $file2.tmp | wc -l
            rm $file2.tmp
        done

function finish {
    rm file1.tmp
}
trap finish EXIT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文