Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to process a file that contains a specific character in the middle of some words and move it to the beggining of that word. It could also appear at the beggining of a word, but in that case, it should remain in the original position. The special character is ~. I mean:

Original File:

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.

Expected result:

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.

Can this be done with a sed script?

Thank in advance!

share|improve this question
add comment

2 Answers

$ 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.
share|improve this answer
add comment

This might work for you:

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.