1

ファイル1:

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行)にフォーマットする必要があります。どうすればこれを達成できますか?単語はどれも同じではなく、上記の構造だけが同じです。結果のファイルは次のようになります。

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が通常の候補です。

4

6 に答える 6

3

そして、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

また

# 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
于 2009-10-24T11:56:01.690 に答える
2
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   -
于 2009-10-24T11:05:40.323 に答える
2

perlワンライナー:

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 

これは、必要な出力の「ようなもの」です。

于 2009-10-24T11:18:20.067 に答える
1

使用するスクリプト言語がわからない場合は、ここに擬似コードを入力してください。

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
于 2009-10-24T10:48:03.170 に答える
1

このワンライナーを試してみてください。この1つのライナーは、常に1単語あたり6行になるという条件で機能します。

sed 'N;N;N;N;N;N;N;N;s/\n/ /g' test_3
于 2009-10-24T11:49:05.323 に答える
1
sed -ne'1{x;d};/^-$/{g;s/\n/ /g;p;n;x;d};H'
awk -v'RS=\n-\n' '{gsub(/\n/," ")}1'
于 2009-10-24T18:04:41.707 に答える