2014-01-26 1 views
0

두 개 이상의 입력 파일을 하나의 출력 파일로 결합하는 솔루션을 찾고 있습니다. 작동 방식은 'diff -U 999999 file1.txt file2.txt> output.txt'와 동일하지만 diff 표시기없이 수행됩니다.'diff - unified'와 같은 파일을 어떻게 결합합니까?

+0

은은 File2.txt 및 중복 file2.txt인가를? 예를 들어, file1.txt에는 먼저 uniqe가 들어 있고, 다음은 file2.txt에 공통적 인 내용이 들어 있습니다. 반면에 file2.txt는 공통 부분에 고유 한 항목이 오는 것으로 시작합니다. 내가 묻는 이유는 만약 그렇다면 내가 해결책이 있다고 생각한다. – hlovdal

+0

Hello hlovdal, 예, 두 파일이 공통된 부분을 공유 할 수 있습니다. 사실 두 개의 로그 파일을 병합하고 싶습니다. 중복되는 부분이있을 수 있으며 한 번 나타나야합니다. – user3236483

답변

0

다음은 이전에 여러 로그 파일을 병합하기 위해 작성한 스크립트입니다. 나는 우리의 로그 printf("time(NULL) = %d\n", time(NULL));의 결과를 정기적으로 발생을 포함

... 작은 파일에 대한 잘 작동하지만 고통스럽게하고 결국 누적 된 로그가 큰 성장으로 unusably 느린가 된 수동 첫번째 kdiff3 사용하기 시작, 당신은 몇 가지를 찾을 적응해야 다른 단조 증가 싱크 마크.

#!/usr/bin/perl 
use strict; 
use warnings; 

# This program takes two overlapping log files and combines 
# them into one, e.g. 
# 
#   INPUT:     OUTPUT: 
# 
# file1  file2    combined 
# AAA        AAA 
# AAA        AAA 
# AAA        AAA 
# BBB   BBB     BBB 
# BBB   BBB     BBB 
# BBB   BBB     BBB 
#     CCC     CCC 
#     CCC     CCC 
#     CCC     CCC 
#     CCC     CCC 
# 

# This programm uses the "time(NULL) = <...time...>" lines in the 
# logs to match where the logs start overlapping. 

# Example line matched with this function: 
# time(NULL) = 1388772638 
sub get_first_time_NULL { 
    my $filename = shift; 
    my $ret = undef; 
    open(FILE, $filename); 
    while (my $line = <FILE>) { 
     if ($line =~ /^time\(NULL\) = (\d+)/) { 
      $ret = $1; 
      last; 
     } 
    } 
    close(FILE); 
    return $ret; 
} 

my $F1_first_time = get_first_time_NULL($ARGV[0]); 
my $F2_first_time = get_first_time_NULL($ARGV[1]); 

my $oldest_file; 
my $newest_file; 
my $newest_file_first_time; 

if ($F1_first_time <= $F2_first_time) { 
    $oldest_file = $ARGV[0]; 
    $newest_file = $ARGV[1]; 
    $newest_file_first_time = $F2_first_time; 
} else { 
    $oldest_file = $ARGV[1]; 
    $newest_file = $ARGV[0]; 
    $newest_file_first_time = $F1_first_time; 
} 

# Print the "AAA" part 
open(FILE, $oldest_file); 
while (my $line = <FILE>) { 
    print $line; 
    last if ($line =~ /^time\(NULL\) = $newest_file_first_time/); 
} 
close(FILE); 

# Print the "BBB" and "CCC" parts 
my $do_print = 0; 
open(FILE, $newest_file); 
while (my $line = <FILE>) { 
    print $line if $do_print; 
    $do_print = 1 if ($line =~ /^time\(NULL\) = $newest_file_first_time/); 
} 
close(FILE); 

위 펄 스크립트는 두 개의 파일을 처리하는, 그래서 한 번의 작업으로 모든 로그 파일을 처리하기 위해 다음과 같은 쉘 스크립트 작성 :

#!/bin/sh 

# This script combines several overlapping logfiles into one 
# continous one. See merge_log_files.pl for more details into 
# how the logs are merged, this script is only glue to process 
# multiple files in one operation. 

set -e 

MERGE_RESULT="$1" 
shift 

echo "Processing $1..." 
cp "$1" MeRgE.TeMp.1 
shift 

while [ -n "$1" ] 
do 
    if [ ! -s "$1" ] 
    then 
     echo "Skipping empty file $1..." 
     shift 
     continue 
    fi 
    echo "Processing $1..." 
    perl `echo $0 | sed 's/\.sh$/.pl/'` MeRgE.TeMp.1 "$1" > MeRgE.TeMp.2 && mv MeRgE.TeMp.2 MeRgE.TeMp.1 
    shift; 
done 

mv MeRgE.TeMp.1 $MERGE_RESULT 
echo "Done" 
관련 문제