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.

5
  • 6
    canna reproduce, works fine on linux Commented Feb 22, 2014 at 5:03
  • If you are using Mac OSX, the sed comes from BSD and not GNU. You can read about differences here: stackoverflow.com/questions/6761361/… Commented Feb 22, 2014 at 5:15
  • no, I am not using Mac Commented Feb 22, 2014 at 5: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. Commented Feb 22, 2014 at 5:30
  • it may be helpful to tell us that language that is Commented Feb 22, 2014 at 11:35

3 Answers 3

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'
Sign up to request clarification or add additional context in comments.

1 Comment

after executing sed 's/(.)/\1 /g' getting the same output as mentioned in question
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.

6 Comments

It is showing the output but many of the garbage characters are also introduced
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ɪ
-1 Can you explain why the sed solution doesn't work? This is a heck of a lot more complicated.
I am new to these commands. I don't know , I got somewhere this sed command that I am using.
The encoding may be due to your terminal. Please see here: stackoverflow.com/questions/1820659/…
|
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"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.