打印具有正确头部的文件名 [英] Print names of files with proper head

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

问题描述

我想获取当前目录中文件的名称,使文件的第一行等于 myWord.我想结合 find .-type f 命令与 -exec 选项与 head -1 文件名 但无济于事.这个问题有什么聪明的单线解决方案吗?

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?

推荐答案

你可以使用 findawk:

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

awk 命令将在每个文件的第一行搜索单词 myWord 然后打印该文件名.

awk command will search first line of each file for the word myWord then it prints that filename.

或者使用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天全站免登陆