shell比对文本文件
1,对于文件中的字符是以换行结尾的如下:
[root@py test]# cat 1.txt 11 22 aa 33 cc [root@py test]# cat 2.txt 11 cc dd ee ff 使用如下
[root@py test]# cat 2.sh
while read line1dowhile read line2doif [ $line1 = $line2 ]thenecho $line1fidone <1.txtdone <2.txt
[root@py test]# ./2.sh 11 cc
2.如果文本文件是以空格或者制表符分隔的,如下:
可以使用如下方法:
bb dd 当然这个方法也可以针对第一种方法,但是貌似效率没有第一种方法高.!for i in `cat 1.txt`dofor j in `cat 2.txt`doif [ $i = $j ]thenecho $ifidonedone
3.对于以其他分隔符分隔的,可以使用awk先来过滤,例如以
[root@py ~]# cat 1.txt aa,bb,cc,dd,ee [root@py ~]# cat 2.txt 11,22,bb,33,44,dd
for x in `awk -F, '{for(i=1;i<=NF;i++){print $i}}' 1.txt`dofor y in `awk -F, '{for(i=1;i<=NF;i++){print $i}}' 2.txt`doif [ $x = $y ]thenecho $xfidonedone
bb dd
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
