求助 comm命令

sh/bash/dash/ksh/zsh等Shell脚本
回复
wy200747055
帖子: 2
注册时间: 2012-03-27 9:55

求助 comm命令

#1

帖子 wy200747055 » 2012-03-27 10:16

本人用的是ubuntu8.04(不要鄙视版本低,这个版本在虚拟机中上网木问题的啊,高版本总有问题),知道comm命令是比较两个文件的,在终端试了一下,结果见图,我看书上介绍的是:comm结果会有三行,第一行显示第一个文件特有的行,第二行显示第二个文件特有的行,第三行显示两个文件共有的行。可是介绍的跟结果差得很大啊,到底怎么理解comm命令啊,已经困扰了半天了,急~
附件
无标题.png
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 求助 comm命令

#2

帖子 aerofox » 2012-03-27 10:30

代码: 全选

SYNOPSIS

comm [OPTION]... FILE1 FILE2  
DESCRIPTION

Compare sorted files FILE1 and FILE2 line by line.
特别注意那个 sorted!
头像
Think1st
帖子: 45
注册时间: 2012-02-07 23:08

Re: 求助 comm命令

#3

帖子 Think1st » 2012-03-28 23:02

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

Re: 求助 comm命令

#4

帖子 Think1st » 2012-03-29 22:30

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
在这里,问题比答案更抢手。
回复