아래 이전 줄의 일부를 복사하세요.

아래 이전 줄의 일부를 복사하세요.

다음과 같은 구조의 파일이 있습니다.

GO:0000001      mitochondrion inheritance
GO:0000002      mitochondrial genome maintenance
GO:0000003      reproduction
alt_id: GO:0019952
alt_id: GO:0050876
GO:0000005      obsolete ribosomal chaperone activity
GO:0000006      high-affinity zinc uptake transmembrane transporter activity
GO:0000007      low-affinity zinc ion transmembrane transporter activity
GO:0000008      obsolete thioredoxin
alt_id: GO:0000013
GO:0000009      alpha-1,6-mannosyltransferase activity

그것이 말하는 것은 이전 코드를 대체한다는 것을 alt_id의미합니다 . 이전 정의의 정의를 GO:추가하고 싶습니다 . 즉, 다음과 같은 출력을 원합니다.alt_idGO:

GO:0000001      mitochondrion inheritance
GO:0000002      mitochondrial genome maintenance
GO:0000003      reproduction
alt_id: GO:0019952     reproduction
alt_id: GO:0050876     reproduction
GO:0000005      obsolete ribosomal chaperone activity
GO:0000006      high-affinity zinc uptake transmembrane transporter activity
GO:0000007      low-affinity zinc ion transmembrane transporter activity
GO:0000008      obsolete thioredoxin
alt_id: GO:0000013      obsolete thioredoxin
GO:0000009      alpha-1,6-mannosyltransferase activity

아래 이전 줄의 내용을 어떻게 복사할 수 있나요? Windows 기반 환경에서 Cygwin을 사용하고 있습니다.

답변1

을 사용 awk하면 작동할지 확실하지 않습니다.Cygwin

$ awk '{ if(/^alt_id/){$0 = $0" "p} else{p = ""; for (i=2; i<=NF; i++) p = p" "$i} } 1' ip.txt
GO:0000001      mitochondrion inheritance
GO:0000002      mitochondrial genome maintenance
GO:0000003      reproduction
alt_id: GO:0019952  reproduction
alt_id: GO:0050876  reproduction
GO:0000005      obsolete ribosomal chaperone activity
GO:0000006      high-affinity zinc uptake transmembrane transporter activity
GO:0000007      low-affinity zinc ion transmembrane transporter activity
GO:0000008      obsolete thioredoxin
alt_id: GO:0000013  obsolete thioredoxin
GO:0000009      alpha-1,6-mannosyltransferase activity
  • alt_id행의 시작 부분에서 일치하지 않는 각 행에 대해 변수( p)를 사용하여 두 행에서 시작하는 모든 열을 저장합니다.
  • 줄의 시작 부분에서 줄이 일치하면 변수 에 포함된 입력 줄 alt_id에 변수의 내용을 추가합니다.p$0
  • 마지막으로 1콘텐츠 인쇄 바로가기입니다.$0

답변2

이 작업은 다음을 통해 쉽게 수행할 수 있습니다.sed

sed '
    N  #append next line (operate with `line1\nline2`);
    /\nalt_id/s/\([^0-9]*\)\n.*/&\1/
       #if next line starts with `alt_id` the append end of present line
    P  #print present line (all before `\n`)
    D  #remove all before `\n`, starts from begin with remain part (line2)
    ' file

또 다른 방법은 예약된 공간을 사용하는 것입니다.

sed '
    /^alt_id:/G #if line starts by `alt_id:` append hold-space
    s/\n//      #remove `\n`ewline symbol
    t           #if removing success pass further commands (go to end)
    h           #if no (for other lines) copy it to hold-space
    s/\S*//     #remove all non-space symbols from start till first space
    x           #exchange hold-space and pattern-space ==
                #+put resedue into hold-space and return full line
    ' file

관련 정보