我想找到目录中的所有文本文件不要以字符串结尾:
hello world
我怎样才能做到这一点?
答案1
尝试:
for f in *; do
if [ -f "$f" ] && [ "$(tail -n1 -- "$f")" != "hello world" ]; then
printf '%s\n' "$f"
fi
done
答案2
尝试一下这个经过测试的版本:
#!/usr/bin/awk -f
BEGIN {
last=FILENAME;
}
{
if (last != FILENAME) {
if (line !~ /^hello world$/ && line != "hello world") {
print last;
}
last=FILENAME;
}
line=$0;
}
END {
if (line !~ /^hello world$/ && line != "hello world") {
print FILENAME;
}
}
考试:
$ chmod +x script.awk
$ ls
script.awk test1.txt test2.txt test3.txt
$ ./script.awk $(find . -type f -exec file {} + | fgrep text | cut -d: -f1)
./script.awk
./test1.txt
答案3
cd /my/directory/path
for file in *
do
grep -v '^$' < "${file}" | tail -1 | grep "hello world" > /dev/null; result=${?}
if [ "${result}" -ne 0 ]
then
echo "${file}"
fi
done
该grep -v '^$' < "${file}"
部分消除了可能添加到末尾的空行。如果不需要,您可以省略该部分并将该行作为tail -1 < "${file}"