이전 행 내용의 일부를 다음 행에 복사합니다.

이전 행 내용의 일부를 다음 행에 복사합니다.

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

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_id이전 정의를 추가하고 싶습니다 GO:. 즉, 다음과 같은 출력을 원합니다.

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

관련 정보