0

次の形式のデータがあります。

<some text0>
<text1> <text2> .
<some text1>
<text3> <text4> .

次に、このデータを次の形式に変換します。

<text1> <text2> <some text0>.
<text3> <text4> <some text1>.

C++ を使用して実行できることはわかっていますが、Linux を使用してこれを行う方法はありますか。sed が文字の置換に優れていることは知っていますが、sed を使用して上記の形式の置換を行う方法についてはわかりません。

4

5 に答える 5

2

このワンライナーはあなたの例で機能します:

kent$  awk '!/\.$/{s=$0;next}sub(/\.$/,s".")' f
<text1> <text2> <some text0>.
<text3> <text4> <some text1>.

説明:

awk               #the program
!/\.$/{s=$0;next} #if the line was not ending with dot(.),
                  #assign it to s, read next line
sub(/\.$/,s".")   #we are here when the line ends with ".",
                  #then we replace ending "." with s, and print.
f                 #the input file
于 2013-11-06T12:53:02.333 に答える
0

レコード区切り記号を次のように再定義することによりgawk:

$ awk 'NR>1{print $1,$2,R $3}{R=RT}' RS='<some text[^>]>' file
<text1> <text2> <some text0>.
<text3> <text4> <some text1>.
于 2013-11-06T12:59:32.373 に答える
0

私は awk を使用しますが、これは対照的な長いパイプラインです

sed 's/\.$//' <<END | tac | paste -d " " - - | tac | sed 's/$/./'
<some text0>
<text1> <text2> .
<some text1>
<text3> <text4> .
END    sed 's/\.$//' <<END | tac | paste -d " " - - | tac | sed 's/$/./'
<some text0>
<text1> <text2> .
<some text1>
<text3> <text4> .
END
<text1> <text2>  <some text0>.
<text3> <text4>  <some text1>.
于 2013-11-06T13:00:09.630 に答える