shell : 解析文件并判断模式是否存在
shell : parse file and tell if pattern exist or not
我正在尝试制作一个非常简单的脚本来解析文件,然后告诉我我要查找的字符串是否存在。
我可以逐行读取 txt 文件,然后使用 grep
。但是我无法测试字符串是否不存在,我也不知道为什么。
#!/bin/bash
cat file.txt | grep '<span>my name is john</span>' -i | while IFS= read line ; do
if test -z "$line"
then
echo "$line is empty" <--- Can't get here
else
echo "$line is NOT empty"
fi
done
如果你想看看哪些行行,哪些行不行 -
while read line # simplistic - see other posts on handling with more finesse
do case "$line" in # replaces grep
*"$yourString"*) echo "found" ;;
*) echo "none" ;;
esac
done < file.txt # no need for cat
或者,
grep -i '<span>my name is john</span>' file.txt
给你所有的命中,
grep -iv '<span>my name is john</span>' file.txt
给你所有的非命中。否则,您可能应该在输出中添加更多信息以使其有用。
我正在尝试制作一个非常简单的脚本来解析文件,然后告诉我我要查找的字符串是否存在。
我可以逐行读取 txt 文件,然后使用 grep
。但是我无法测试字符串是否不存在,我也不知道为什么。
#!/bin/bash
cat file.txt | grep '<span>my name is john</span>' -i | while IFS= read line ; do
if test -z "$line"
then
echo "$line is empty" <--- Can't get here
else
echo "$line is NOT empty"
fi
done
如果你想看看哪些行行,哪些行不行 -
while read line # simplistic - see other posts on handling with more finesse
do case "$line" in # replaces grep
*"$yourString"*) echo "found" ;;
*) echo "none" ;;
esac
done < file.txt # no need for cat
或者,
grep -i '<span>my name is john</span>' file.txt
给你所有的命中,
grep -iv '<span>my name is john</span>' file.txt
给你所有的非命中。否则,您可能应该在输出中添加更多信息以使其有用。