다른 열에서 값을 추출하고 다른 열에서 바꾸기

다른 열에서 값을 추출하고 다른 열에서 바꾸기

나는 이것을 해야 한다:

##fsdfsd
##sdd-ver gen 5.5.7
Xm Gen CDS     1       148     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     149     193     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     194     279     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     280     412     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     413     499     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     500     702     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen extracted region        1       148     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="437225 <- 437372";ID=Bm
Xm Gen extracted region        149     193     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="436969 <- 437013";ID=Bm
Xm Gen extracted region        194     279     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="435418 <- 435503";ID=Bm
Xm Gen extracted region        280     412     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="435209 <- 435341";ID=Bm
Xm Gen extracted region        413     499     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="434376 <- 434462";ID=Bm
Xm Gen extracted region        500     702     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="434084 <- 434286";ID=Bm

(Xm Gen CDS) 행을 (Xm Gen 추출 영역) 행에 있는 값으로 바꿉니다. 즉, 첫 번째 행($4 열:1은 437225 값으로 대체되고 $5 열:148은 437372로 대체되고, 행 2에서는($4 열:149가 436969로 대체되고 $5 열:193이 437013으로 대체되는 등) 출력은 다음과 같습니다. 아래에

##gff-version 2
##source-version geneious 5.5.7
Xm Gen CDS     437225       437372     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     436969     437013     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     435418     435503     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     435209     435341     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     434376     434462     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen CDS     434084     434286     .       +       .       Name=;created by=User;modified by=User;ID=Bm
Xm Gen extracted region        1       148     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="437225 <- 437372";ID=Bm
Xm Gen extracted region        149     193     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="436969 <- 437013";ID=Bm
Xm Gen extracted region        194     279     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="435418 <- 435503";ID=Bm
Xm Gen extracted region        280     412     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="435209 <- 435341";ID=Bm
Xm Gen extracted region        413     499     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="434376 <- 434462";ID=Bm
Xm Gen extracted region        500     702     .       +       .       Name=Extracted region from gi|371442828|gb|JH557032.1|;Extracted interval="434084 <- 434286";ID=Bm

답변1

약간 복잡한 변형이지만 꽤 잘 작동합니다.

head -2 file 
join <(grep "Xm Gen CDS" file | cat -n) \
     <(grep "Xm Gen extracted region" file | cat -n) | \
     sed 's/^[0-9]* //;s/CDS [0-9]*\s[0-9]*\(\s.*interval="\([0-9]*\)\s<-\s\([0-9]*\)\)/CDS\t\2\t\3\t\1/;s/ Xm Gen extracted.*//'
grep "Xm Gen extracted region" file

쉘 스크립트로 실행하려면

#!/bin/bash
FILE="$1"
head -2 "$FILE"
join <(grep "Xm Gen CDS" "$FILE" | cat -n) \
     <(grep "Xm Gen extracted region" "$FILE" | cat -n) | \
     sed 's/^[0-9]* //;s/CDS [0-9]*\s[0-9]*\(\s.*interval="\([0-9]*\)\s<-\s\([0-9]*\)\)/CDS\t\2\t\3\t\1/;s/ Xm Gen extracted.*//'
grep "Xm Gen extracted region" "$FILE"

관련 정보