无法过滤包含“是目录”的行 通过 SED/AWK

发布于 2024-07-15 03:51:33 字数 893 浏览 7 评论 0原文

我运行代码给了我以下示例数据

md5deep find * | awk '{ print $1 }'

输出示例

    /Users/math/Documents/Articles/Number theory: Is a directory
    258fe6853b1bfb2d07f512ff6bec52b1
    /Users/math/Documents/Articles/Probability and statistics: Is a directory
    4811bfb2ad04b9f4318049c01ebb52ef
    8aae4ac3694658cf90005dbdea37b4d5
    258fe6853b1bfb2d07f512ff6bec52b1

我尝试通过SED过滤包含是一个目录的行但未成功

md5deep find * | awk '{ print $1 }' | sed s/\/*//g

它的示例输出是

/Users/math/Documents/Articles/Number theory: Is a directory
/Users/math/Documents/Articles/Topology: Is a directory
/Users/math/Documents/Articles/useful: Is a directory

如何通过 SED/AWK 筛选出包含“Is a directory”的每一行?

[说明] 我想过滤掉包含是一个目录的行。

I run the code gives me the following sample data

md5deep find * | awk '{ print $1 }'

A sample of the output

    /Users/math/Documents/Articles/Number theory: Is a directory
    258fe6853b1bfb2d07f512ff6bec52b1
    /Users/math/Documents/Articles/Probability and statistics: Is a directory
    4811bfb2ad04b9f4318049c01ebb52ef
    8aae4ac3694658cf90005dbdea37b4d5
    258fe6853b1bfb2d07f512ff6bec52b1

I have tried to filter the rows which contain Is a directory by SED unsuccessfully

md5deep find * | awk '{ print $1 }' | sed s/\/*//g

Its sample output is

/Users/math/Documents/Articles/Number theory: Is a directory
/Users/math/Documents/Articles/Topology: Is a directory
/Users/math/Documents/Articles/useful: Is a directory

How can I filter Out each row which contains "Is a directory" by SED/AWK?

[clarification]
I want to filter out the rows which contain Is a directory.

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

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

发布评论

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

评论(4

池予 2024-07-22 03:51:34

为什么不使用 grep 来代替呢?

即,

md5deep find * | grep "Is a directory" | awk '{ print $1 }'

编辑:我刚刚重新阅读了您的问题,如果您想删除 Is a directory 行,请使用 grep 的 -v 标志,即:

md5deep find * | grep -v "Is a directory" | awk '{ print $1 }'

Why not use grep instead?

ie,

md5deep find * | grep "Is a directory" | awk '{ print $1 }'

Edit: I just re-read your question and if you want to remove the lines with Is a directory, use the -v flag of grep, ie:

md5deep find * | grep -v "Is a directory" | awk '{ print $1 }'
姜生凉生 2024-07-22 03:51:34

我对 md5deep 不太熟悉,但这可能会做一些像你想做的事情。

find -type f -exec md5sum {} +

I'm not intimately familiar with md5deep, but this may do something like you are tying to do.

find -type f -exec md5sum {} +
邮友 2024-07-22 03:51:33

我没有使用过 md5deep 工具,但我相信这些行是错误消息; 他们将进入标准错误而不是标准输出,因此他们将直接进入您的终端而不是通过管道。 因此,它们不会被您的 sed 命令过滤。 您可以通过合并标准错误和标准输出流来过滤它们,但

看起来(我不确定,因为您缺少反引号)您尝试调用

md5deep `find *`

并查找正在返回所有文件和目录。

关于您可能想要执行的操作的一些注释:

  • 看起来 md5deep 有一个 -r 表示“递归”选项。 所以,您可能想尝试:

    <前><代码>md5deep -r *

    而不是 find 命令。

  • 如果您确实希望使用 find 命令,则可以使用 -type f 将其限制为仅查找文件,而不是文件和目录。 另外,您不需要将 * 传递到 find 命令中(如果存在名称类似于 find 的选项的文件,这可能会混淆 find 理解); 传入 . 将在当前目录中递归搜索。

    <前><代码>查找 . -f型

  • sed 中,如果您希望在模式中使用斜杠,则使用 \ 正确引用它们可能会很痛苦。 您可以选择不同的字符来分隔正则表达式; sed 将使用 s 命令后的第一个字符作为分隔符。 您的模式还缺少 .; 在正则表达式中,要指示任何字符的一个实例,请使用 .,要指示“零个或多个前面的表达式”,请使用 *,因此 . * 表示“零个或多个任何字符”(这与 glob 模式不同,其中 * 单独表示“零个或多个任何字符”)。

    sed "s|/.*||g" 
      
  • 如果您确实希望将标准错误流包含在标准输出中,以便它将通过管道,那么您可以运行:

    md5deep `find *` 2>&1 |   awk ...  
      
  • 如果您只想忽略 stderr,则可以将其重定向到 /dev/null,这是一个特殊文件,只会丢弃其中的任何内容:

    md5deep `find *` 2>/dev/null |   awk ... 
      

总之,我认为下面的命令将帮助您解决当前的问题,如果我不明白您在寻找什么,上面列出的其他建议可能会对您有所帮助:

md5deep -r * | awk '{ print $1 }'

I have not used the md5deep tool, but I believe those lines are error messages; they would be going to standard error instead of standard out, and so they are going directly to your terminal instead of through the pipe. Thus, they won't be filtered by your sed command. You could filter them by merging your standard error and standard output streams, but

It looks like (I'm not sure because you are missing the backquotes) you are trying to call

md5deep `find *`

and find is returning all of the files and directories.

Some notes on what you might want to do:

  • It looks like md5deep has a -r for "recursive" option. So, you may want to try:

    md5deep -r *
    

    instead of the find command.

  • If you do wish to use a find command, you can limit it to only files using -type f, instead of files and directories. Also, you don't need to pass * into a find command (which may confuse find if there are files that have names that looks like the options that find understands); passing in . will search recursively through the current directory.

    find . -type f
    
  • In sed if you wish to use slashes in your pattern, it can be a pain to quote them correctly with \. You can instead choose a different character to delimit your regular expression; sed will use the first character after the s command as a delimiter. Your pattern is also lacking a .; in regular expressions, to indicate one instance of any character you use ., and to indicate "zero or more of the preceding expression" you use *, so .* indicates "zero or more of any character" (this is different from glob patterns, in which * alone means "zero or more of any character").

    sed "s|/.*||g"
    
  • If you really do want to be including your standard error stream in your standard output, so it will pass through the pipe, then you can run:

    md5deep `find *` 2>&1 | awk ... 
    
  • If you just want to ignore stderr, you can redirect that to /dev/null, which is a special file that just discards anything that goes into it:

    md5deep `find *` 2>/dev/null | awk ...
    

In summary, I think the command below will help you with your immediate problem, and the other suggestions listed above may help you if I did not undersand what you were looking for:

md5deep -r * | awk '{ print $1 }'
策马西风 2024-07-22 03:51:33

具体回答澄清:如何使用 awk 和 sed 过滤行:

awk '/Is a directory/ {next} {print}'
sed 'g/Is a directory/d'

To specifically answer the clarification: how to filter out lines using awk and sed:

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