sed의 특정 문자를 처음으로 이동 in the middle

일부 단어의 중간에 특정 문자가 포함 된 파일을 처리하고 해당 단어의 시작 부분으로 옮기고 싶습니다. 또한 단어의 시작 부분에 나타날 수도 있지만,이 경우 원래 위치에 남아 있어야합니다. 특수 문자는 ~입니다. 내말은:

원본 파일 :

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.

예상 결과:

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.

이것은 sed 스크립트로 할 수 있습니까?



답변

$ 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.

답변

이것은 당신을 위해 일할 수도 있습니다 :

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.