use*_*743 10 bash grep command-line
在 linux shell 中,我想确保一组特定的文件都以 开头<?
,具有确切的字符串并且开头没有其他字符。我怎样才能 grep 或使用其他一些来表达“文件开头”?
编辑:我是通配符,并head
没有在同一行给出文件名,所以当我 grep 它时,我看不到文件名。此外,"^<?"
似乎没有给出正确的结果;基本上我得到这个:
$> head -1 * | grep "^<?"
<?
<?
<?
<?
<?
...
Run Code Online (Sandbox Code Playgroud)
所有的文件实际上都很好。
jan*_*sen 11
在 Bash 中:
for file in *; do [[ "$(head -1 "$file")" =~ ^\<\? ]] || echo "$file"; done
确保它们是文件:
for file in *; do [ -f "$file" ] || continue; [[ "$(head -1 "$file")" =~ ^\<\? ]] || echo "$file"; done
执行以下操作grep
:
$ head -n 1 * | grep -B1 "^<?"
==> foo <==
<?
--
==> bar <==
<?
--
==> baz <==
<?
Run Code Online (Sandbox Code Playgroud)
解析出文件名:
$ head -n 1 * | grep -B1 "^<?" | sed -n 's/^==> \(.*\) <==$/\1/p'
foo
bar
baz
Run Code Online (Sandbox Code Playgroud)