Mover personagem específico em sed para o começo

1

Eu gostaria de processar um arquivo que contém um caractere específico no meio de algumas palavras e movê-lo para o início dessa palavra. Ele também pode aparecer no início de uma palavra, mas, nesse caso, deve permanecer na posição original. O caractere especial é ~. Quero dizer:

Arquivo original:

This is a sentence with the cha~racter in the middle of a word.
This is a sentence with the ~character at the beginning of a word.
This is a sentence without the character.

Resultado esperado:

This is a sentence with the ~character in the middle of a word.
This is a sentence with the ~character at the begining of a word.
This is a sentence without the character.

Isso pode ser feito com um script sed?

Chesús
fonte

Respostas:

2
$ sed 's/\b\(\w\+\)~\(\w\+\)\b/~\1\2/g' <<< 'This is a sentence with the cha~racter in the middle of a word.
> This is a sentence with the ~character at the beggining of a word.
> This is a sentence without the character.'
This is a sentence with the ~character in the middle of a word.
This is a sentence with the ~character at the beggining of a word.
This is a sentence without the character.
Ignacio Vazquez-Abrams
fonte
0

Isso pode funcionar para você:

sed 's/\<\([^~][^ ~]*\)~/~\1/g' file
This is a sentence with the ~character in the middle of a word.
This is a sentence with the ~character at the beggining of a word.
This is a sentence without the character.
potong
fonte