File 使用Unix shell脚本在另一个文本文件中搜索文本文件的每一行
我想使用Unix shell脚本在另一个文本文件中搜索文本文件的每一行。考虑这两个文本文件: 文件1:File 使用Unix shell脚本在另一个文本文件中搜索文本文件的每一行,file,unix,comparison,File,Unix,Comparison,我想使用Unix shell脚本在另一个文本文件中搜索文本文件的每一行。考虑这两个文本文件: 文件1: HELEN JOE FRED HARRY TERRY 文件2: hello FRED bert JOE hi there HARRY JOHN PETE was here hello STEVE 所需的输出应如下所示: HELEN not found in file2 JOE found in file2 FRED found
HELEN
JOE
FRED
HARRY
TERRY
文件2:
hello FRED
bert
JOE
hi there
HARRY
JOHN
PETE was here
hello STEVE
所需的输出应如下所示:
HELEN not found in file2
JOE found in file2
FRED found in file2
HARRY found in file2
TERRY not found in file2
我使用的脚本如下:
while read name
do
found="no"
while read line
do
echo $line | grep $name >/dev/null
if [ $? -eq 0 ]
then
echo $name found in file2
found="yes"
break
fi
done < file2
if [ $found = "no" ]
then
echo $name not found in file2
fi
done < file1
此处使用的脚本或逻辑是否有问题?这可以完成以下任务:
while read f; do
if grep -q -o $f file2
then
echo $f found in file2
else
echo $f not fount in file2
fi
done<file1
读取f时;做
如果grep-q-o$f文件2
然后
在文件2中找到echo$f
其他的
echo$f未出现在文件2中
fi
Doney你的代码对我来说可以正常工作。。。i、 e.它显示了您在所需输出下拥有的内容。我正在操作系统上使用bash-3.2.57X@hharishh你用哪种外壳?
while read f; do
if grep -q -o $f file2
then
echo $f found in file2
else
echo $f not fount in file2
fi
done<file1