Frage

File1:

hello
- dictionary definitions:
hi
hello
hallo
greetings
salutations
no more hello for you
-
world
- dictionary definitions:
universe
everything
the globe
the biggest tree
planet
cess pool of organic life
-

Ich brauche diese zu formatieren (für eine große Liste von Worten) in einen Begriff zu Definition-Format (eine Zeile pro Semester). Wie kann man das erreichen? Keines der Wörter sind die gleichen, nur die Struktur oben gesehen ist. Die resultierende Datei in etwa so aussehen:

hello    - dictionary definitions:    hi    hello    hallo    greetings    salutations    no more hello for you    -
world    - dictionary definitions:    universe    everything    the globe    the biggest tree    planet    cess pool of organic life    -

Awk / Sed / Grep / Katze sind die üblichen Anwärter.

War es hilfreich?

Lösung

awk 'BEGIN {FS="\n"; RS="-\n"}{for(i=1;i<=NF;i++) printf("%s   ",$i); if($1)print"-";}' dict.txt

Ausgänge:

hello   - dictionary definitions:   hi   hello   hallo   greetings   salutations   no more hello for you   -
world   - dictionary definitions:   universe   everything   the globe   the biggest tree   planet   cess pool of organic life   -

Andere Tipps

und wer sagt nur Perl es elegant tun? :)

$ gawk -vRS="-\n" '{gsub(/\n/," ")}1' file
hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you
world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life

oder

# gawk 'BEGIN{RS="-\n";FS="\n";OFS=" "}{$1=$1}1'  file
hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you
world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life

Ein Perl-Einzeiler:

perl -pe 'chomp;s/^-$/\n/;print " "' File1

gibt

 hello - dictionary definitions: hi hello hallo greetings salutations no more hello for you
 world - dictionary definitions: universe everything the globe the biggest tree planet cess pool of organic life 

Das ist 'so etwas wie' Ihre gewünschte Ausgabe.

Nicht sicher, dass die Skriptsprache Sie verwenden werden, Pseudo-Code hier:

for each line
 if line is "-"
  create new line
 else
  append separator to previous line
  append line to previous line
 end if
end for loop

Versuchen Sie dieses Motto auf einer Bedingung arbeitet, dass theer immer 6 Zeilen für ein Wort sein wird,

sed 'N;N;N;N;N;N;N;N;s/\n/ /g' test_3
sed -ne'1{x;d};/^-$/{g;s/\n/ /g;p;n;x;d};H'
awk -v'RS=\n-\n' '{gsub(/\n/," ")}1'
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top