2009-10-24 4 views
1

Fichier1:reformater un grand fichier texte dans une des chaînes de ligne (via BASH)

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 ce (pour une liste énorme de mots) en un terme au format de définition (une ligne par terme) . Comment peut-on y parvenir? Aucun des mots n'est le même, seule la structure vue ci-dessus est. Le fichier résultant ressemblerait à quelque chose comme 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.

+0

question a été modifiée pour être plus spécifique à la langue/commande de script. – user191960

Répondre

2
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 - 
+0

Je t'aime mec! C'est une commande énorme de friggin '- et cela fonctionne parfaitement. – user191960

+0

Notez que si vous devez gérer la ligne vide à la fin du fichier, vous devrez ajouter un if: 'awk 'BEGIN {FS =" \ n "; RS = "- \ n"} {if (NF> 2) {pour (i = 1; i <= NF; i ++) printf ("% s", $ i); print ("-");}} ' dict.txt' –

+0

Merci pour le conseil RC. – user191960

1

Pas sûr que le langage de script que vous utiliserez, le code de pseudo 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 
2

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.

+0

Bien! Plus élégant que celui ci-dessus. J'ai entendu dire que perl est génial pour ses capacités de manipulation de texte. – user191960

+0

Perl est génial, aussi awk, son grand-père :) – ghostdog74

+0

Oui, Larry Wall a certainement donné crédit à 'awk', aucun doute à ce sujet. – pavium

1

Essayez cette ligne de travaille sur une des conditions qui theer seront toujours 6 lignes pour un mot

sed 'N;N;N;N;N;N;N;N;s/\n/ /g' test_3 
+0

pas assez flexible. vous ne saurez jamais combien de définitions il y a – ghostdog74

3

et qui dit que 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 
+0

Vous devez faire attention aux lignes qui se terminent par '-' avec RS réglé comme ça ... – ephemient

+0

ne comprends pas. est-ce bar ? ou juste ? – ghostdog74

1
sed -ne'1{x;d};/^-$/{g;s/\n/ /g;p;n;x;d};H' 
awk -v'RS=\n-\n' '{gsub(/\n/," ")}1' 
Questions connexes