Question

Fichier1:

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
-

Je dois formater ceci (pour une longue liste de mots) en un format de définition de terme (une ligne par terme). Comment peut-on y arriver? Aucun des mots n'est identique, seule la structure vue ci-dessus l'est. Le fichier résultant ressemblerait à ceci:

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 sont les prétendants habituels.

Était-ce utile?

La solution

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

sorties:

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   -

Autres conseils

et qui dit que seul Perl peut le faire avec élégance? :)

$ 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

OU

# 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

Un perl one-liner:

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

donne

 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 

Ceci est "quelque chose comme" votre sortie requise.

Vous n'êtes pas sûr du langage de script que vous allez utiliser, pseudo-code ici:

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

Essayez cette doublure qui fonctionne à la condition que le mot soit toujours composé de 6 lignes

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'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top