用适当的头打印文件的名称 [英] Print names of files with proper head

查看:141
本文介绍了用适当的头打印文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前目录中文件的名称,使得文件的第一行等于 myWord 。我想结合 find。用 -exec 选项加上头文件但是无济于事。有没有一些巧妙的,单线解决这个问题?

I want to get the name of files in current directory such that the first line of the file is equal to myWord. I wanted to combine find . -type f command with -exec option with head -1 filename but to no avail. Is there some clever, one-line solution to this problem?

推荐答案

您可以使用 find with awk

find . -maxdepth 1 -type f -exec awk '/myWord/{print FILENAME} {nextfile}' {} +



awk命令将在每个文件的第一行搜索单词 myWord 然后输出该文件名。

或者使用 gnu sed

Or using gnu sed:

find . -type f -exec sed '/myWord/F;Q' {} \;

非常感谢@chepner和@ 123在这个答案下的非常有用的评论。

Many thanks to @chepner and @123 for extremely helpful comments below this answer.

这篇关于用适当的头打印文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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