- 论坛徽章:
- 0
|
文件a.txt、b.txt内容如下
#more a.txt
11
22
33
#more b.txt
11
33
要求取出a.txt中存在, b.txt中不存在的内容,这里为 22
方法1 --- grep
#grep -v -f b.txt a.txt
22
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The empty
file contains zero patterns, and therefore matches
nothing.
-v, --invert-match
Invert the sense of matching, to select non-matching lines.
方法2 -- 脚本(未验证)
#!/bin/ksh
#
#
function Usage {
echo "Usage: dfile [-x|-y|-z] file1 file2"
}
(($# != 3 )) && { Usage; exit 1; }
for i in $(cat $2);do
for j in $(cat $3);do
echo $i $j
done
done|tee a$$ b$$>/dev/null
same=$(awk '$1==$2 {print $1}' a$$)
X=$(echo "两个文件相同的行是:"echo $same|tr ' ' '\n')
diff1=$(awk '$1 != $2 {print $1}' b$$|uniq -d )
Y=$(echo "文件$2中存在而文件$3中不存在的行是:"
echo $same $diff1|tr ' ' '\n'|sort|uniq -u
echo "\n")
for n in $(cat $3);do
for m in $(cat $2);do
echo $n $m
done
done|tee x$$ y$$>/dev/null
diff2=$(awk '$1 != $2 {print $1}' y$$|uniq -d )
Z=$(echo "在文件$3中存在而文件$2中不存在的行是:"
echo -n $diff2 $same|tr ' ' '\n'|sort|uniq -u
echo "\n")
while getopts xyz arg
do
case $arg in
x) echo $X|tr ' ' '\n';;
y) echo $Y|tr ' ' '\n';;
z) echo $Z|tr ' ' '\n';;
\?) echo "选项错误"; Usage;;
esac
done
rm a$$ b$$ x$$ y$$
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/70323/showart_1150749.html |
|