Mover caractere 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?

    
por Chesús 20.03.2012 / 14:28

2 respostas

2

$ sed 's/\b\(\w\+\)~\(\w\+\)\b/~/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.
    
por 20.03.2012 / 14:56
0

Isso pode funcionar para você:

sed 's/\<\([^~][^ ~]*\)~/~/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.
    
por 22.03.2012 / 15:52

Tags