分页: 1 / 1

求助 comm命令

发表于 : 2012-03-27 10:16
wy200747055
本人用的是ubuntu8.04(不要鄙视版本低,这个版本在虚拟机中上网木问题的啊,高版本总有问题),知道comm命令是比较两个文件的,在终端试了一下,结果见图,我看书上介绍的是:comm结果会有三行,第一行显示第一个文件特有的行,第二行显示第二个文件特有的行,第三行显示两个文件共有的行。可是介绍的跟结果差得很大啊,到底怎么理解comm命令啊,已经困扰了半天了,急~

Re: 求助 comm命令

发表于 : 2012-03-27 10:30
aerofox

代码: 全选

SYNOPSIS

comm [OPTION]... FILE1 FILE2  
DESCRIPTION

Compare sorted files FILE1 and FILE2 line by line.
特别注意那个 sorted!

Re: 求助 comm命令

发表于 : 2012-03-28 23:02
Think1st
wy200747055 写了:本人用的是ubuntu8.04(不要鄙视版本低,这个版本在虚拟机中上网木问题的啊,高版本总有问题),知道comm命令是比较两个文件的,在终端试了一下,结果见图,我看书上介绍的是:comm结果会有三行,第一行显示第一个文件特有的行,第二行显示第二个文件特有的行,第三行显示两个文件共有的行。可是介绍的跟结果差得很大啊,到底怎么理解comm命令啊,已经困扰了半天了,急~
column:列

Re: 求助 comm命令

发表于 : 2012-03-29 22:30
Think1st
wy200747055 写了:本人用的是ubuntu8.04(不要鄙视版本低,这个版本在虚拟机中上网木问题的啊,高版本总有问题),知道comm命令是比较两个文件的,在终端试了一下,结果见图,我看书上介绍的是:comm结果会有三行,第一行显示第一个文件特有的行,第二行显示第二个文件特有的行,第三行显示两个文件共有的行。可是介绍的跟结果差得很大啊,到底怎么理解comm命令啊,已经困扰了半天了,急~
用脚本实现comm(果断想多了。。 :em06 ),算法估计是酱紫。。

代码: 全选

#!/bin/bash

list1="\n"
list2="\n"
list3="\n"
exec 3< file1
exec 4< file2

while :
do
	read line1 <&3
	read line2 <&4
	[[ "$line1" == "" && "$line2" == "" ]] && break

	if [[ "$line1" == "" ]]
	then
		while [[ "$line2" != "" ]]
		do
			list2=$list2$line2"\n"
			read line2 <&4
		done
		break
	fi

	while [[ "$line2" != "" && "$line2" != "$line1" ]]
	do
		list2=$list2$line2"\n"
		read line2 <&4
	done

	if [[ "$line2" == "" ]]
	then
		while [[ "$line1" != "" ]]
		do
			list1=$list1$line1"\n"
			read line1 <&3
		done
		break
	else
		list3=$list3$line1"\n"
		continue
	fi
		
done

printf "list1 belongs to file1: $list1"
printf "list2 belongs to file2: $list2"
printf "list3 belongs to file1 and file2: $list3"

exec 4<&-
exec 3<&-
执行后结果
list1 belongs to file1:
hello,ubuntu
hello,linux
hello,everyone
list2 belongs to file2:
hello,ubuntu
hello,kitt
hello,everyone
list3 belongs to file1 and file2:
hello,world