2016-08-03 2 views
2

와 AWK (둔한)를 비교 : (은 File2.txt)파일을 열 내 2 개 개의 입력 파일에서 refinment

a 11-23 
b 33-39 
c 40-45 
d 46-58 

& (file2.txt)

33-39 
40-42 
43-47 
51-52 

나는 파일 2 일치 할 필요 FILE1의 두 번째 항목의 값과 출력 (중간 범위에 대한 확인과 함께) 및 원하는이 될 같은

b 33-39 
c 40-42, 43-45 
d 46-47, 51-52 

'd 46-47, 51-52'는 마지막 줄에 file2의 43-47 범위로 나타나며 c와 d에 해당합니다.

스택 오버플 사용자 karakfa가 정상적으로 방법처럼 제안

는 :

b 33-39 
c 40-45, 40-42 
d 46-58, 51-52 

그렇지만, 'C 40-45'및 전체 범위 값 '

$ join -j 99 file1 file2 | 
    awk '$2==$3{print $1,$2; next} {split($2,a,"-"); split($3,b,"-")} 
    a[1]>=b[1] && a[2]<=b[2] || a[1]<=b[1] && a[2]>=b[2] {print $1,$2",",$3}' 

는 같은 출력을 제공한다 d 46-58 '은 내가 원하는 범위가 아니 었으며, 또한 내 Windows 컴퓨터에서 실행하기 위해 GNU Awk와 호환되도록해야합니다. TXR에서

+0

는'awk' 여기에 작업에 가장 .. – sjsam

+1

가 그리고 당신은 자신이 무엇을하려고 했습니까? awk 문서를 보았습니까? 배열의 작동 방식, 배열을 통해 루프를 만드는 방법, 여러 파일 작업 방법을 이해 했습니까? –

답변

0

솔루션 :

@(do 
    (defstruct interval nil 
    lo hi 

    (:postinit (self) 
     (unless (< self.lo self.hi) 
     (swap self.lo self.hi))) 

    (:method print (self stream) 
     (put-string `@{self.lo}[email protected]{self.hi}` stream)) 

    (:method intersects (self other) 
     (and (>= self.hi other.lo) 
      (<= self.lo other.hi))) 

    (:method clip (self other) 
     (new interval 
      lo (max self.lo other.lo) 
      hi (min self.hi other.hi)))) 

    (defstruct tabentry nil 
    label 
    interval 
    matches 

    (:method insert (self other) 
     (when self.interval.(intersects other) 
     (push self.interval.(clip other) self.matches))))) 
@(next "file1.txt") 
@(collect :vars (tab)) 
@label @[email protected] 
@ (bind tab @(new tabentry 
        label label 
        interval (new interval 
           lo (int-str lo) 
           hi (int-str hi)))) 
@(end) 
@(next "file2.txt") 
@(repeat) 
@[email protected] 
@ (do 
    (let ((iv (new interval 
        lo (int-str lo) 
        hi (int-str hi)))) 
     (each ((te tab)) 
     te.(insert iv)))) 
@(end) 
@(do (each ((te tab)) 
     (when te.matches 
     (put-line `@{te.label} @{(reverse te.matches) ", "}`)))) 

실행 : 나는 의심

$ txr tab-range.txr 
b 33-39 
c 40-42, 43-45 
d 46-47, 51-52 
관련 문제