繰り返しなしでテキストをトリミング

繰り返しなしでテキストをトリミング

入力(変数$ VAR形式):

'yoo' : x'yoo' 'welcome' : x'welcome' 'we' : x'we' 'dum' : x'dum' 'test' : x'test' 'poo' : x'poo' 'D2-dog' : x'D2-dog' 'ant' : x'ant' 'rat' : x'rat' 'xmass' : x'xmass'

私は出力が次のようになります:

yoo welcome we dum test poo D2-dog ant rat xmass

私は出力が次のようにしたくありません。

yoo
welcome
we
dum
test
poo
D2-dog
ant
rat
xmass

試してみましたが、 echo $VAR | sed "s/' : x'//g" 混乱して予想していたものとは異なります。

cutortrまたはを使用できますかsed

Alexの回答後の編集:他のテキストに問題があります。

echo "'yoo' : x'yoo' 'welcome' : x'welcome' 'we' : x'we' 'test' : x'test' 'poo' : x'poo' 'dog' : x'dog' 'ant' : x'ant' 'rat' : x'rat' 'xmass' : x'xmass' 'exp' : x'exp' 'S-123': x'S-123' 'demo': x'demo' 'neu': x'neu'" | sed -e "s/'\([^']*\)' : x'\1'/\1/g"

私が得た結果は次のとおりです。

yoo welcome we test poo dog ant rat xmass exp 'S-123': x'S-123' 'demo': x'demo' 'neu': x'neu'

おかしい、問題を把握できない!

ベストアンサー1

sed -e "s/'\([^']*\)' \?: x'\1'/\1/g"

次のように実行します。

$ MYVAR="'yoo' : x'yoo' 'welcome' : x'welcome' 'we' : x'we' 'dum' : x'dum' 'test' : x'test' 'poo' : x'poo' 'D2-dog' : x'D2-dog' 'ant' : x'ant' 'rat' : x'rat' 'xmass' : x'xmass'"
$ echo "$MYVAR" | sed -e "s/'\([^']*\)' \?: x'\1'/\1/g"
yoo welcome we dum test poo D2-dog ant rat xmass

\Xこれは、逆参照が拡張検索パターンの一部として表示される可能性があるためです。

編集:RexExpを変更して、コロンの前のスペースをオプションにしました。

おすすめ記事