Переформатировать большой текстовый файл в одну строку (через BASH) - PullRequest
1 голос
/ 24 октября 2009

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
-

Мне нужно отформатировать это (для огромного списка слов) в формате термин-определение (одна строка на термин). Как можно этого достичь? Ни одно из слов не одно и то же, только структура, представленная выше. Результирующий файл будет выглядеть примерно так:

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 - обычные соперники.

Ответы [ 6 ]

3 голосов
/ 24 октября 2009

а кто сказал, что только Perl может сделать это элегантно? :)

$ 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
2 голосов
/ 24 октября 2009

Перл однострочный:

perl -pe 'chomp;s/^-$/\n/;print " "' 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 

Это «что-то вроде» вашего требуемого вывода.

2 голосов
/ 24 октября 2009
awk 'BEGIN {FS="\n"; RS="-\n"}{for(i=1;i<=NF;i++) printf("%s   ",$i); if($1)print"-";}' dict.txt

выходы:

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   -
1 голос
/ 24 октября 2009
sed -ne'1{x;d};/^-$/{g;s/\n/ /g;p;n;x;d};H'
awk -v'RS=\n-\n' '{gsub(/\n/," ")}1'
1 голос
/ 24 октября 2009

Попробуйте этот лайнер работает при условиях, что всегда будет 6 строк для слова

sed 'N;N;N;N;N;N;N;N;s/\n/ /g' test_3
1 голос
/ 24 октября 2009

Не уверен, что вы будете использовать язык сценариев, псевдокод здесь:

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
...