UNIX“通讯”实用程序允许在 BSD 中不区分大小写,但在 Linux 中不允许(通过 -i 标志)。我怎样才能在 Linux 中得到它?

发布于 2024-08-19 19:12:32 字数 1187 浏览 12 评论 0原文

我在 BSD 平台 (OSX) 上开发的应用程序中使用了出色的 UNIX“comm”命令行实用程序。当我部署到我的 Linux 生产服务器时,我遗憾地发现 Ubuntu Linux 的“comm”实用程序没有使用 -i 标志来指示应该不区分大小写地比较这些行。显然 POSIX 标准不需要 -i 选项。

所以...我陷入了困境。我确实需要在 BSD 上运行良好的 -i 选项。我已经尝试在 Linux 机器上编译 BSD comm.c 源代码,但我得到:

http://svn.freebsd.org/viewvc/base/user/luigi/ipfw3-head/usr .bin/comm/comm.c?view=markup&pathrev=200559

me@host:~$ gcc comm.c 
comm.c: In function ‘getline’:
comm.c:195: warning: assignment makes pointer from integer without a cast
comm.c: In function ‘wcsicoll’:
comm.c:264: warning: assignment makes pointer from integer without a cast
comm.c:270: warning: assignment makes pointer from integer without a cast
/tmp/ccrvPbfz.o: In function `getline':
comm.c:(.text+0x421): undefined reference to `reallocf'
/tmp/ccrvPbfz.o: In function `wcsicoll':
comm.c:(.text+0x691): undefined reference to `reallocf'
comm.c:(.text+0x6ef): undefined reference to `reallocf'
collect2: ld returned 1 exit status

有人对如何在 Linux 上获取支持“comm -i”的 comm 版本有任何建议吗?

谢谢!

I'm using the excellent UNIX 'comm' command line utility in an application which I developed on a BSD platform (OSX). When I deployed to my Linux production server I found out that sadly, Ubuntu Linux's 'comm' utility does not take the -i flag to indicate that the lines should be compared case-insensitive. Apparently the POSIX standard does not require the -i option.

So... I'm in a bind. I really need the -i option that works so well on BSD. I've gone so far to try to compile the BSD comm.c source code on the Linux box but I got:

http://svn.freebsd.org/viewvc/base/user/luigi/ipfw3-head/usr.bin/comm/comm.c?view=markup&pathrev=200559

me@host:~$ gcc comm.c 
comm.c: In function ‘getline’:
comm.c:195: warning: assignment makes pointer from integer without a cast
comm.c: In function ‘wcsicoll’:
comm.c:264: warning: assignment makes pointer from integer without a cast
comm.c:270: warning: assignment makes pointer from integer without a cast
/tmp/ccrvPbfz.o: In function `getline':
comm.c:(.text+0x421): undefined reference to `reallocf'
/tmp/ccrvPbfz.o: In function `wcsicoll':
comm.c:(.text+0x691): undefined reference to `reallocf'
comm.c:(.text+0x6ef): undefined reference to `reallocf'
collect2: ld returned 1 exit status

Does anyone have any suggestions as to how to get a version of comm on Linux that supports 'comm -i'?

Thanks!

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

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

发布评论

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

评论(4

情痴 2024-08-26 19:12:32

您可以在 comm.c 中添加以下内容:

void *reallocf(void *ptr, size_t size)
{
    void *ret = realloc(ptr, size);
    if (ret == NULL) {
        free(ptr);
    }
    return ret;
}

然后您应该能够编译它。确保comm.c中包含#include(它可能已经这样做了)。

编译失败的原因是 BSD comm.c 使用 reallocf() 这不是标准 C 函数。但写起来很容易。

You can add the following in comm.c:

void *reallocf(void *ptr, size_t size)
{
    void *ret = realloc(ptr, size);
    if (ret == NULL) {
        free(ptr);
    }
    return ret;
}

You should be able to compile it then. Make sure comm.c has #include <stdlib.h> in it (it probably does that already).

The reason your compilation fails is because BSD comm.c uses reallocf() which is not a standard C function. But it is easy to write.

夜空下最亮的亮点 2024-08-26 19:12:32

@OP,没有必要花那么大的篇幅来进行您自己的 src 代码编译。这是一个替代建议。由于您希望不区分大小写,因此在将文件传递给 comm 之前,您可以使用其他工具(例如 tr)将两个文件中的大小写转换为小写(或大写)。

tr '[A-Z]' '[a-z]' <file1 > temp1
tr '[A-Z]' '[a-z]' <file2 > temp2
comm temp1 temp2

@OP ,there's no need to go to such length as to do your own src code compilation . Here's an alternative suggestion. Since you want case insensitive, you can just convert the cases in both files to lower (or upper case) using another tool such as tr before you pass the files to comm.

tr '[A-Z]' '[a-z]' <file1 > temp1
tr '[A-Z]' '[a-z]' <file2 > temp2
comm temp1 temp2
背叛残局 2024-08-26 19:12:32

您可以尝试 cat 这两个文件并将它们通过管道传输到 uniq -c -i。它将显示两个文件中的所有行,并在第一列中显示出现次数。只要原始文件没有重复的行,则第一列 >1 的所有行都是两个文件共有的行。

希望有帮助!

You could try to cat both files and pipe them to uniq -c -i. It'll show all lines in both files, with the number of appearances in the first column. As long as the original files don't have repeated lines, all lines with the first column >1 are lines common to both files.

Hope it helps!

萌酱 2024-08-26 19:12:32

有人对如何在 Linux 上获取支持“comm -i”的 comm 版本有任何建议吗?

不完全是这样;但是您是否检查过 join 实用程序是否可以满足您的要求?这个确实在 Linux 上有 -i 选项...

Does anyone have any suggestions as to how to get a version of comm on Linux that supports 'comm -i'?

Not quite that; but have you checked if your requirements could be satisfied by the join utility? This one does have the -i option on Linux...

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