2017-10-03 8 views
-2

두 번째 열의 내용 중 일부가 공백으로 구분 된 두 개의 열 사이에 구분 된 입력 파일이 탭으로 구분되어 있습니다 (예 : "LEA type "은 동일한 두 번째 열에 완전히 속하지만"LEA "는 두 번째 열에 입력하고"type "은 세 번째 열에 올리며 마찬가지로"Ribosomal protein L21P "는 두 번째 열에 들어가야하지만 같은 이름이됩니다. 두 번째, 세 번째 및 네 번째 열. "LEA 타입이"이동하지 않는이 "LEA_type"위치 및 다른 셀의 컨텐츠와 같은 열 두이어야되도록나머지 열의 셀을 이동하지 않고 특정 행의 두 열의 내용을 병합하는 방법

1st_col  2nd_col  3rd_col 4th_col 5th_col 6th_col 
tATAAAta TBP   ~   1  
tACCAT  Ribosomal protein  L21P ~ 2 
agtACCAT Ribosomal protein  L21P ~ 2 
ATGTActt AP2   ~   1  
GCAACggagc LEA   type  1  ~ 1 
ATGGTa  Ribosomal protein  L21P ~ 1 
ATGGTctt Ribosomal protein  L21P ~ 2 
ATGGTaca Ribosomal protein  L21P ~ 1 

원하는 OUPUT이 같을 sholud.

1st_col  2nd_col     3rd_col 4th_col 5th_col 6th_col 
tATAAAta TBP      ~  1 
tACCAT  Ribosomal_protein_L21P ~  2 
agtACCAT Ribosomal_protein_L21P ~  2 
ATGTActt AP2      ~  1 
GCAACggagc LEA_type    ~  1 
ATGGTa  Ribosomal_protein_L21P ~  1 
ATGGTctt Ribosomal_protein_L21P ~  2 
ATGGTaca Ribosomal_protein_L21P ~  1 

나는이 같은 시도를했지만 다른 셀도 이동시킵니다.

sed 's/LEA\stype/LEA_type/g' 1_com_final_2922.txt | sed 's/Ribosomal\sprotein/Ribosomal_protein/g' 

미리 감사드립니다.

답변

0

귀하의 질문은 100 % 명확하지 않지만, 출력 및 설명 된 조건에 따라 LEA, 유형 및 Ribosomal, 단백질, L21P 문자열을 찾고 표시된 결과에 따라 결합합니다.

awk '($2=="LEA" && $3=="type"){$2="LEA_type";$3=""} ($2=="Ribosomal" && $3=="protein" && $4=="L21P"){$2="Ribosomal_protein_L21P";$3=$4=""} 1' Input_file 

출력은 다음과 같습니다.

tATAAAta TBP ~ 1 Ca_00015 Ca_00015 
0 0 0 0 Ca_00027 Ca_00027 
atTTACCgaa Trihelix ~ 2 Ca_00027 Ca_00027 
0 0 0 0 Ca_00027 Ca_00027 
0 0 0 0 Ca_00027 Ca_00027 
tACCAT Ribosomal_protein_L21P ~ 2 
agtACCAT Ribosomal_protein_L21P ~ 2 
GCAACggagc LEA_type 1 ~ 1 
ATGGTa Ribosomal_protein_L21P ~ 1 
ATGGTctt Ribosomal_protein_L21P ~ 2 
ATGGTaca Ribosomal_protein_L21P ~ 1 
GCAACctccc LEA_type 1 ~ 1 

솔루션 중 하나가 아닌 라이너 형태 추가.

awk ' 
($2=="LEA" && $3=="type"){ 
    $2="LEA_type"; 
    $3="" 
} 
($2=="Ribosomal" && $3=="protein" && $4=="L21P"){ 
    $2="Ribosomal_protein_L21P"; 
    $3=$4="" 
} 
1 
' Input_file 

편집 : OP 이후 조금 그래서 조금으로 코드를 변경 요청 변경도 다음과 같습니다. 또한 Input_file이 TAB로 구분 된 경우 awk -F "\ t"를 사용하십시오.

awk ' 
($2=="LEA" && $3=="type"){ 
    $2="LEA_type"; 
    $3=$4=""; 
} 
($2=="Ribosomal" && $3=="protein" && $4=="L21P"){ 
    $2="Ribosomal_protein_L21P"; 
    $3=$4=""; 
} 
1 
' Input_file | column -t 

출력은 다음과 같습니다.

1st_col  2nd_col     3rd_col 4th_col 5th_col 6th_col 
tATAAAta TBP      ~  1 
tACCAT  Ribosomal_protein_L21P ~  2 
agtACCAT Ribosomal_protein_L21P ~  2 
ATGTActt AP2      ~  1 
GCAACggagc LEA_type    ~  1 
ATGGTa  Ribosomal_protein_L21P ~  1 
ATGGTctt Ribosomal_protein_L21P ~  2 
ATGGTaca Ribosomal_protein_L21P ~  1 
+0

덕분에 @ RavinderSingh13, 그것도 세포의 위치를 ​​이동으로 문제를 해결할 수 없다, 그냥 더 나은 이해를 위해 내 예제를 업데이 트 다시 한번 봐. –

+0

그래,이 샘플 파일에서 잘 작동하지만, 실제 크기의 파일에서이 모든 파일을 실행하려고하면 모든 열이 함께 병합됩니다. 탭으로 구분 된 출력 파일을 넣을 수 있습니까? –

+0

탭을 넣어야합니다. 귀하의 게시물에 이상적으로는 작동해야합니다. – RavinderSingh13

0

여기

awk '$2~/[^0-9|^~]+/{for(i=3;i<=NF;i++){ if($i~/[^0-9|^~]+/){ $2=sprintf("%s_%s",$2,$i); $i="" } else break } }1' file 

,

awk '$2~/[^0-9|^~]+/{     # search the line which $2 is not numeric nor tide 
    for(i=3;i<=NF;i++){    # continue to search start from $3 
    if($i~/[^0-9|^~]+/){   # if $i is not numeric nor tide 
     $2=sprintf("%s_%s",$2,$i); # substitute $2 as $2_$i 
     $i=""      # set $i="" 
    } else      # if hit something numeric or tide, we break 
     break 
    } 
}1' 

여기에 한 줄의, 그 작업을 수행하는보다 유연한 방법 편집 :

업데이트 된 OP에 대한 대답을 업데이트,

awk '$3~/[^~]/ && NR>1{for(i=3;i<=NF;i++){ if($i~/[^0-9|^~]+/){ $2=sprintf("%s_%s",$2,$i); $i="" } else{$3="~"; $4=$(i+1); $i=""; $(i+1)=""; break} } }1' file5 | column -t 
+0

@CWLiu에게 감사드립니다. 문제가 해결되지 않으면 방금 예를 업데이트했습니다. 감사 –