无法过滤包含“是目录"的行通过 SED/AWK [英] Unable to filter rows which contain "Is a directory" by SED/AWK

查看:39
本文介绍了无法过滤包含“是目录"的行通过 SED/AWK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I run the code gives me the following sample data

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 过滤包含 Is a directory 的行,但未成功

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

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 的行.

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

推荐答案

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

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 *`

并且 find 正在返回所有文件和目录.

and find is returning all of the files and directories.

关于您可能想要做什么的一些说明:

Some notes on what you might want to do:

  • 看起来 md5deep 有一个 -r 表示递归"选项.因此,您可能想尝试:

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

md5deep -r *

代替 find 命令.

instead of the find command.

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

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

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

  • 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 ... 
    

  • 如果你只想忽略 stderr,你可以将它重定向到 /dev/null,这是一个特殊的文件,它只会丢弃任何进入其中的内容:

  • 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 }'
    

    这篇关于无法过滤包含“是目录"的行通过 SED/AWK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆