Answers:
在Bash中:
for file in *; do [[ "$(head -1 "$file")" =~ ^\<\? ]] || echo "$file"; done
确保它们是文件:
for file in *; do [ -f "$file" ] || continue; [[ "$(head -1 "$file")" =~ ^\<\? ]] || echo "$file"; done
find
还可以直接返回纯文件以直接启动管道。
read
代替Bash head
: for file in *; do [ -f "$file" ] || continue; read < "$file"; [[ "$REPLY" =~ ^\<\? ]] || echo "$file"; done
做grep
:
$ head -n 1 * | grep -B1 "^<?"
==> foo <==
<?
--
==> bar <==
<?
--
==> baz <==
<?
解析出文件名:
$ head -n 1 * | grep -B1 "^<?" | sed -n 's/^==> \(.*\) <==$/\1/p'
foo
bar
baz
尝试这个
for i in `find * | grep "php$"`; do echo -n $i " -> "; head -1 $i; done
这将获取以PHP结尾的每个文件的列表,然后循环遍历。回显文件名,然后打印文件的第一行。我刚插入
将为您提供如下输出:
calendar.php -> <?php
error.php -> <?php
events.php -> <?php
gallery.php ->
index.php -> <?php
splash.php -> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
information.php -> <?php
location.php -> <?php
menu.php -> <?php
res.php -> <?php
blah.php -> <?php
那么您可以在末尾粘贴普通的grep来摆脱您想看到的内容并找到例外
for i in `find * | grep "php$"`; do echo -n $i " -> "; head -1 $i; done | grep -v "<?php"
输出:
gallery.php ->
splash.php -> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
for x in *.php;do echo $x \"
头-n1 $ x\";done
cat file.txt | head -1 | grep "^<?"
应该按照您的要求做。
cat
使用head -1 file.txt | grep "^<?"
就足够了。
command
将文件作为参数。可能不是绝对必要,但我没有
find