user77
user77

Reputation: 33

linux command for introducing single space after each character in text file

I have a text file and I have to introduce single space after each character. The command that I am using is:

sed 's/./& /g' source.txt>output.txt

But I am not getting the output, output remains same as input. Please help me regarding this. Is this command correct?

external edit: according to the comment below, the input text is: 'pustə́_pɾemí nù_kədé ví_kɪse_pyaːɾe mɪ'

and its corrsponding output is : pustə́_pɾemí nù_kədé ví_kɪse_pyaːɾe mɪ

Instead of introducing single space after each character it is replacing single space(which is already in input)with double space.

Upvotes: 1

Views: 650

Answers (3)

philshem
philshem

Reputation: 25331

Your sed command works for me, but the accents on the characters are moved from the character.

Here is a longer sed command that you can try, and it also works for me.

sed 's/\(.\)/\1 /g'

Upvotes: 1

jaypal singh
jaypal singh

Reputation: 77105

Your sed command is correct so it appears to me that you are running in to some locale issue.

On the prompt type the following and re-run your command:

LANG="en_US.UTF-8"

Upvotes: 0

philshem
philshem

Reputation: 25331

Here is an 'awk' option if 'sed' doesn't work

awk 'BEGIN{FS=OFS=""}{for (i=1;i<=NF;i++) {$i=$i " "}}1' source.txt > output.txt

modified from here

edit: If you are using Mac OSX, 'sed' comes from BSD and not GNU. You can read here instructions how to add a single space and then modify to fit your needs.

Upvotes: 0

Related Questions