Reputation: 2011
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
-
I need to format this (for a huge list of words) into a term to definition format (one line per term). How can one achieve this? None of the words are the same, only the structure seen above is. The resultant file would look something like this:
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 are the usual contenders.
Upvotes: 1
Views: 1802
Reputation: 204926
sed -ne'1{x;d};/^-$/{g;s/\n/ /g;p;n;x;d};H'
awk -v'RS=\n-\n' '{gsub(/\n/," ")}1'
Upvotes: 1
Reputation: 342799
and who says only Perl can do it elegantly ? :)
$ 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
OR
# 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
Upvotes: 3
Reputation: 67301
Try this one liner works on a conditions that theer will always be 6 lines for a word
sed 'N;N;N;N;N;N;N;N;s/\n/ /g' test_3
Upvotes: 1
Reputation: 15128
A perl one-liner:
perl -pe 'chomp;s/^-$/\n/;print " "' File1
gives
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
This is 'something like' your required output.
Upvotes: 2
Reputation:
awk 'BEGIN {FS="\n"; RS="-\n"}{for(i=1;i<=NF;i++) printf("%s ",$i); if($1)print"-";}' dict.txt
outputs:
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 -
Upvotes: 2
Reputation: 25820
Not sure the scripting language you will be using, pseudo code here:
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
Upvotes: 1