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?

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.

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.
Ao utilizar nosso site, você reconhece que leu e compreendeu nossa Política de Cookies e nossa Política de Privacidade.
Licensed under cc by-sa 3.0 with attribution required.