简体   繁体   English

如何在第一行上打印包含“#!bin / bash”的文件的名称

[英]How to print the name of the files that contains “#!bin/bash” on the first line

I tried to print the name of the files that contains #!bin/bash on the first line (the extension doesn't matter) that are in the same file. 我试图在同一文件的第一行上打印包含#!bin/bash的文件的名称(扩展名无关紧要)。

I did this which allow me to print the name of the files that contains : #!bin/bash everywhere in the file, but I only need to print the name of the files that contains this chain on the first line. 我这样做是为了让我可以在文件的所有位置打印包含#!bin/bash的文件的名称,但是我只需要在第一行上打印包含此链的文件的名称。

Here is what I did : 这是我所做的:

find . -type f -name - grep -L '#!bin/bash' {} \; -printf '%f\n'> file.txt

What about using awk for this? 那用awk呢?

awk 'NR==1 && ($0 == "#!bin/bash") {print FILENAME} {exit}' file

This checks if the first line has this content. 这将检查第一行是否包含此内容。 If so, it prints the filename. 如果是这样,它将打印文件名。 Then, no matter what happened before, it exits the file since it is not needed to process it anymore. 然后,无论之前发生了什么,它都会退出文件,因为不再需要对其进行处理。

Using find to provide the files to the awk command, just say find /your/path -exec awk '...' {} \\; 使用find将文件提供给awk命令,只需说find /your/path -exec awk '...' {} \\; such as: 如:

find -type f -exec awk 'NR==1 && ($0 == "#!bin/bash") {print FILENAME} {exit}' {} \;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM