无法过滤包含“是目录”的行 通过 SED/AWK
我运行代码给了我以下示例数据
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不使用 grep 来代替呢?
即,
编辑:我刚刚重新阅读了您的问题,如果您想删除 Is a directory 行,请使用 grep 的 -v 标志,即:
Why not use grep instead?
ie,
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 不太熟悉,但这可能会做一些像你想做的事情。
I'm not intimately familiar with md5deep, but this may do something like you are tying to do.
我没有使用过 md5deep 工具,但我相信这些行是错误消息; 他们将进入标准错误而不是标准输出,因此他们将直接进入您的终端而不是通过管道。 因此,它们不会被您的 sed 命令过滤。 您可以通过合并标准错误和标准输出流来过滤它们,但
看起来(我不确定,因为您缺少反引号)您尝试调用
并查找正在返回所有文件和目录。
关于您可能想要执行的操作的一些注释:
看起来
md5deep
有一个 -r 表示“递归”选项。 所以,您可能想尝试:<前><代码>md5deep -r *
而不是 find 命令。
如果您确实希望使用
find
命令,则可以使用-type f
将其限制为仅查找文件,而不是文件和目录。 另外,您不需要将*
传递到 find 命令中(如果存在名称类似于find 的选项的文件,这可能会混淆
理解); 传入find
.
将在当前目录中递归搜索。<前><代码>查找 . -f型
在
sed
中,如果您希望在模式中使用斜杠,则使用 \ 正确引用它们可能会很痛苦。 您可以选择不同的字符来分隔正则表达式;sed
将使用s
命令后的第一个字符作为分隔符。 您的模式还缺少.
; 在正则表达式中,要指示任何字符的一个实例,请使用.
,要指示“零个或多个前面的表达式”,请使用*
,因此. *
表示“零个或多个任何字符”(这与 glob 模式不同,其中*
单独表示“零个或多个任何字符”)。如果您确实希望将标准错误流包含在标准输出中,以便它将通过管道,那么您可以运行:
如果您只想忽略 stderr,则可以将其重定向到
/dev/null
,这是一个特殊文件,只会丢弃其中的任何内容:总之,我认为下面的命令将帮助您解决当前的问题,如果我不明白您在寻找什么,上面列出的其他建议可能会对您有所帮助:
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, butIt looks like (I'm not sure because you are missing the backquotes) you are trying to call
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: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 confusefind
if there are files that have names that looks like the options thatfind
understands); passing in.
will search recursively through the current directory.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 thes
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").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:
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: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:
具体回答澄清:如何使用 awk 和 sed 过滤行:
To specifically answer the clarification: how to filter out lines using awk and sed: