Pregunta

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
-

Necesito formatear esto (para una gran lista de palabras) en un término para definir el formato (una línea por término). ¿Cómo se puede lograr esto? Ninguna de las palabras es la misma, solo la estructura vista arriba es. El archivo resultante se vería así:

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 / Cat son los contendientes habituales.

¿Fue útil?

Solución

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

salidas:

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   -

Otros consejos

¿y quién dice que solo Perl puede hacerlo con elegancia? :)

$ 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

O

# 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

A perl one-liner:

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

da

 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 

Esto es 'algo así como' tu salida requerida.

No está seguro del lenguaje de script que usará, pseudo código aquí:

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

Intente esto. Un trazador de líneas funciona en las condiciones en que siempre habrá 6 líneas por palabra

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'
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top