Kopieren Sie einen Teil des Inhalts der vorherigen Zeile in die folgende

Kopieren Sie einen Teil des Inhalts der vorherigen Zeile in die folgende

Ich habe eine Datei mit folgender Struktur:

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

Wo es steht, alt_idist es eine Alternative zum vorherigen Code. Ich möchte jedem die Definition des vorherigen GO:hinzufügen , das heißt, ich möchte eine Ausgabe wie diese: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

Wie kann ich den Inhalt der vorherigen Zeile in die folgende kopieren? Ich arbeite mit Cygwin in einer Windows-basierten Umgebung.

Antwort1

Mit awk, nicht sicher, ob es funktioniert aufCygwin

$ 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
  • Für jede Zeile, die nicht alt_idmit dem Zeilenanfang übereinstimmt, verwenden Sie eine Variable ( p), um alle Spalten ab zwei zu speichern.
  • Wenn die Zeile alt_idam Zeilenanfang übereinstimmt, wird der Inhalt der Variablen an die in der Variablen penthaltene Eingabezeile angehängt$0
  • Der letzte 1ist eine Abkürzung zum Drucken des Inhalts von$0

Antwort2

Die Aufgabe kann leicht erledigt werden durchsed

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

Eine andere Möglichkeit besteht darin, die Leertaste zu drücken

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

verwandte Informationen