1

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.

user77
  • 41
  • 1
  • 10
  • 6
    canna reproduce, works fine on linux – iruvar Feb 22 '14 at 05:03
  • If you are using Mac OSX, the sed comes from BSD and not GNU. You can read about differences here: http://stackoverflow.com/questions/6761361/sed-regex-problem-on-mac-works-fine-on-linux – philshem Feb 22 '14 at 05:15
  • no, I am not using Mac – user77 Feb 22 '14 at 05:28
  • input is:- pustə́_pɾemí nù_kədé ví_kɪse_pyaːɾe mɪ on executing sed command output is:- pustə́_pɾemí nù_kədé ví_kɪse_pyaːɾe mɪ .It is not introducing space after each character, but replacing single space (which is already in input) with double space. – user77 Feb 22 '14 at 05:30
  • it may be helpful to tell us that language that is – glenn jackman Feb 22 '14 at 11:35

3 Answers3

1

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'
philshem
  • 24,761
  • 8
  • 61
  • 127
0

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.

Community
  • 1
  • 1
philshem
  • 24,761
  • 8
  • 61
  • 127
  • It is showing the output but many of the garbage characters are also introduced – user77 Feb 22 '14 at 05:19
  • output is like this:- p u s t É Ì _ p É Ÿ e m i Ì n u Ì _ k É d e Ì v i Ì _ k É ª s e _ p y a Ë É Ÿ e m É ª and its corresponding input is:- pustə́_pɾemí nù_kədé ví_kɪse_pyaːɾe mɪ – user77 Feb 22 '14 at 05:20
  • -1 Can you explain why the sed solution doesn't work? This is a heck of a lot more complicated. – John Kugelman Feb 22 '14 at 05:21
  • I am new to these commands. I don't know , I got somewhere this sed command that I am using. – user77 Feb 22 '14 at 05:25
  • The encoding may be due to your terminal. Please see here: http://stackoverflow.com/questions/1820659/how-to-read-files-with-different-encodings-using-awk – philshem Feb 22 '14 at 05:27
  • @JohnKugelman this answer was added before it was known to be a locale issue. The sed command was working for everyone else, so the awk command was an attempt to not try to debug sed. – philshem Feb 22 '14 at 05:37
0

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"
jaypal singh
  • 74,723
  • 23
  • 102
  • 147