Add spaces between Characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add spaces between Characters
# 1  
Old 11-27-2007
Add spaces between Characters

I need to add spaces in between characters in a string variable.

Is there a shortcut? I know you can remove the spaces with sed, but does sed have a way to add them?

Example:
I have: DATA01
I want it to be: D A T A 0 1

What I have done so far is to create a function that will parse thru each character and add a space after, but really would like a one line solution.

Any help would be greatly appreciated.
# 2  
Old 11-27-2007
Code:
echo 'DATA01' | sed 's/./ &/g;s/^ //'

your mileage may vary!
# 3  
Old 11-27-2007
Thanks... it works great! Smilie

But I have to know... what is the sed telling me? I have not worked with one that appears this complicated.

sed 's/./ &/g;s/^ //'
# 4  
Old 11-27-2007
Quote:
Originally Posted by heyindy06
Thanks... it works great! Smilie

But I have to know... what is the sed telling me? I have not worked with one that appears this complicated.

sed 's/./ &/g;s/^ //'
Code:
s/./ &/g

'.' - matches ANY single character
' &' - substitutes a matched expression/character with itself [&] prefixed with a single space

Code:
s/^ //

^ - begining on the line; remove the leading space from the beginning of a line.
# 5  
Old 11-27-2007
With zsh:

Code:
% v="DATA01"                
% print "${${v/// }[2,-1]}" 
D A T A 0 1

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with replacing characters without moving the spaces.

Could you please advise what is the best way to edit a file without disrupting the spaces? For example: I have within my file the value below, wherein I wanted to change VALUE2 to VALUETEST. The total characters on the field of VALUE2 is 15 characters. VALUE1|VALUE2<9 spaces>|VALUE3 but... (3 Replies)
Discussion started by: asdfghjkl
3 Replies

2. Shell Programming and Scripting

Working with lines or variables that have spaces or special characters

Example: while read line do stat -c %G $line done < somefile.txtThe problem is that inside somefile.txt lines can have any symbol allowed as file name, like (). Even with spaces, it splits the words. somefile.txt:dira/my first jump.avi dirb/surf video (1080p).mkv (2 Replies)
Discussion started by: Tribe
2 Replies

3. Shell Programming and Scripting

Removing blank/white spaces and special characters

Hello All , 1. I am trying to do a task where I need to remove Blank spaces from my file , I am usingawk '{$1=$1}{print}' file>file1Input :- ;05/12/1990 ;31/03/2014 ; Output:- ;05/12/1990 ;31/03/2014 ;This command is not removing all spaces from... (6 Replies)
Discussion started by: himanshu sood
6 Replies

4. AIX

Replace all TAB characters with white spaces

Dear Gurus Can you please advise me on how to Replace all TAB characters with white spaces in a text file in AIX? Either using vi or any utilities (2 Replies)
Discussion started by: tenderfoot
2 Replies

5. Shell Programming and Scripting

insert spaces between characters with pure shell

A file contains: abcdef I need : a b c d e f It's easy with sed sed 's/./& /g'but this is embedded linux that doesn't have sed/awk. The shell is unknown but it's bashlike. Parameter expansion works and seems promising and. A question mark seems to work as a wildcard, but there doesn't seem... (5 Replies)
Discussion started by: fubaya
5 Replies

6. UNIX for Dummies Questions & Answers

Line & File Manipulation - add spaces between characters

Is there an awk, sed, vi or any line command that adds Field Separators (default spaces) to each line in a file? $cat RegionalData 12FC2525MZLP8266900216 12FC2525MZLP8266900216 12FC2525NBLP8276900216 12FC2525NBLP8276900216 Desired results: 1 2 F C 2525 MZ LP 826 690 02 16 1 2 F C... (2 Replies)
Discussion started by: MS75001
2 Replies

7. UNIX for Dummies Questions & Answers

replace characters with spaces between tag

I have a file where in some records are having the <Start> and <End> tag. There is data before the start tag , between the tages and after the End tag. I want to replace everything between the start & end tag with equivalent spaces. Input File afsdfaksddfs<start>12678<end>sgdfgdfsf... (6 Replies)
Discussion started by: varunrbs
6 Replies

8. Solaris

removing special characters, white spaces from a field in a file

what my code is doing, it is executing a sql file and the resullset of the query is getting stored in the text file in a fixed format. for that fixed format i have used the following code:: Code: awk -F":"... (2 Replies)
Discussion started by: priyanka3006
2 Replies

9. UNIX for Dummies Questions & Answers

Replace only first found white spaces with some other characters

Anybody can help me How can I replace only four first white spaces with , or any other characters aaaa 08/31/2004 08/31/2009 permanent Logical Processors in System: 64 bedad 08/16/2001 08/15/2011 permanent Logical Processors in System: 64 badnv14 05/31/2008 05/30/2013 permanent Logical... (5 Replies)
Discussion started by: pareshan
5 Replies

10. Programming

Counting characters, words, spaces, punctuations, etc.

I am very new to C programming. How could I write a C program that could count the characters, words, spaces, and punctuations in a text file? Any help will be really appreciated. I am doing this as part of my C learning exercise. Thanks, Ajay (4 Replies)
Discussion started by: ajay41aj
4 Replies
Login or Register to Ask a Question