2013-10-30 21 views
1

假设我有两个文本文件不同之间的两个文本文件

文件1

hello i am John 
    and i live in Cairo 

文件2

hello i am Jogn 
    and i love in Cairo 

,我需要列出词语只(未空间或不同其他任何东西)在两个文本之间得到结果作为文件3,其将包含如下列表中的两个词语

file1  file2 
    John  Jogn 
    live  love 

我该怎么做?

我曾尝试

diff file1 file2 

,但它并不能帮助根据需要

感谢

+0

你必须写一个shell脚本,如果格式是很重要的。 –

+0

什么是'想要的结果'? – stackoverflowuser2010

+0

尝试'diff --suppress-common-lines -side-side',它给出的输出几乎是你想要的格式,你可以在后面添加文件名,也可以在 – abasu

回答

0

使用

awk ' 
    # BEGIN: print 1th & 2th args 
    BEGIN{print ARGV[1], ARGV[2]} 
    # if the current line is from "file1", 
    # put line in the array "a" with the line number for key 
    FNR==NR{a[NR]=$0} 
    if current line is from "file2" 
    FNR!=NR{ 
     # iterate over words of the current line 
     for (i=1; i<=NF; i++) { 
      # split a[key current line] array in array "arr" 
      split(a[FNR], arr) 
      # test if both file1 and file2 Nth element match 
      if (arr[i] != $i) { 
       print arr[i], $i 
      } 
      } 
    } 
' file1 file2 

输出:

/tmp/l1 /tmp/l2 
John Jogn 
live love 
+0

你能解释一下吗? – Edward

+0

发表了相应的评论 –

2

使用wdiff命令得到的结果。

如果你没有它,它在“wdiff”包中,它应该在你系统的仓库中可用。

$ wdiff file1 file2 
hello i am [-John-] {+Jogn+} 
and i [-live-] {+love+} in Cairo 

如果你想有一个图形显示,该meld程序做了很好的工作(安装“合并”包,如果你还没有的话)。

如果您需要特定的输出格式,您需要编写一个脚本。一个好的开始可能是筛选每个输入文件,将每个单词放在一行(fmt -w 1是第一个近似值),然后对结果进行比较。

+0

下工作,但是我怎样才能把结果列在两列中有问题吗? – user1200219

相关问题