bash 如何从标准输入 grep 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26317406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:31:54  来源:igfitidea点击:

How to grep patterns from stdin

bashgrepstdin

提问by Meng Lu

I made a Bash script some time ago to find duplicate files /home/usr/xxx/bin/finddup.sh

我前段时间做了一个 Bash 脚本来查找重复的文件 /home/usr/xxx/bin/finddup.sh

#!/bin/bash
DIR=${1:-`pwd`}
FILENAME=`basename ##代码##`
DATETIME=`date +%Y%m%d%H%M%S`
TMPFILE=`mktemp /tmp/${FILENAME}.${DATETIME}` || exit 1
find -P $DIR -type f -exec cksum {} \; | sort | tee $TMPFILE | cut -f 1-2 -d ' ' | uniq -d | grep -if - $TMPFILE | sort -nr -t' ' -k2,2 | cut -f 3- -d ' ' | while read line; do ls -lhta "$line"; done

But today when I tried to use it again, it didn't work. The problem seems to be from the grep -if - $TMPFILEstep where it failed to find lines of duplicated files from $TMPFILEwith patterns provided from stdin. I cannot figure out the correct usages of -here. Anyone can enlighten me?

但是今天当我再次尝试使用它时,它不起作用。问题似乎出在grep -if - $TMPFILE无法$TMPFILE从标准输入提供的模式中找到重复文件行的步骤。我无法弄清楚-这里的正确用法。任何人都可以启发我吗?

回答by whereswalden

You shouldn't need the -f -at all. If you don't specify a file for grep, it defaults to reading stdin.

你根本不需要-f -。如果您没有为 指定文件grep,则默认为 read stdin

EDIT: this answer is incorrect. -finstructs grepto read patterns from stdininstead of the text to search.

编辑:这个答案是不正确的。-f指示grep从中读取模式stdin而不是要搜索的文本。