有一种解决方案无需循环,可以得到OP想要的结果。
find . -type f -exec sh -c "fgrep print {} /dev/null |tail -1" \;
./tway.pl:print map(lambda x : x[1], filter(lambda x : x[0].startswith('volume'), globals().items()))
./txml.py: print("%s does not exist: %s\n" % (host, error))
./utils.py:print combine_dicts(a, b, operator.mul)
./xml_example.py:print ET.tostring(root, method="text")
与使用 tail -1
相比,每个文件的行数过多,但证明了上述方法可行。
find . -type f -exec sh -c "fgrep print {} /dev/null" \;
给出:
./tway.pl:print map(lambda x : x[1], filter(lambda x : x[0].startswith('volume'), globals().items()))
./txml.py: print("%s resolved to --> %s\n" % (host, ip))
./txml.py: print("%s does not exist: %s\n" % (host, error))
./utils.py:print "a", a
./utils.py:print "b", b
./utils.py:print combine_dicts(a, b, operator.mul)
./xml_example.py: print ">>"
./xml_example.py: print ET.tostring(e, method="text")
./xml_example.py: print "<<"
./xml_example.py:print ET.tostring(root, method="text")
编辑 - 如果您不想在输出中包含文件名,请删除 /dev/null。
tac file | grep -m1 -oP '(?<=tag>).*(?=</tag>)' | head -n 1
甚至使用以下方式grep -m1 -oP '(?<=tag>).*(?=</tag>)' <<(tac file)
- kisna-exec
选项可以做到这一点,即使文件名包含空格/换行符等也可以正常工作... - Sundeep