insert spaces between characters with pure shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert spaces between characters with pure shell
# 1  
Old 03-20-2011
insert spaces between characters with pure shell

A file contains: abcdef
I need : a b c d e f

It's easy with sed
Code:
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 to be an equivalent of the ampersand in the sed example.

Code:
# foo=abcdef

# echo "${foo//?/& }"
& & & & & & 
# echo "${foo//[a-z]/[a-z] }"
[a-z] [a-z] [a-z] [a-z] [a-z] [a-z]

Any other ideas how to do it in pure shell?
# 2  
Old 03-20-2011
Code:
# cat script
#!/bin/bash
if [ $# -ne 1 ] ;then
        echo Usage: $0 [string];exit
fi
for ((s=0;s<${#1};s++))
do
        case $final in
                ^.) final=${1:$s:1};;
                 *) final="$final ${1:$s:1}";;
        esac
done
echo $final

# /temp/script
Usage: /temp/script [string]
# /temp/script abcdef
a b c d e f


Last edited by danmero; 03-20-2011 at 09:59 PM..
This User Gave Thanks to danmero For This Post:
# 3  
Old 03-20-2011
How about this:

Code:
addspace () {
    LEN=${#1}
    for ((c=0;c<$LEN;c++))
    do
       echo -n "${1:$c:1} "
    done
       echo ${1:$LEN:1}
}
addspace "abcdef"

And if your shell dosn't support for loop:
Code:
addspace () {
    LEN=${#1}
        c=0
    while [ $c -lt $LEN ]
    do
       echo -n "${1:$c:1} "
           let c=c+1
    done
    echo ${1:$LEN:1}
}
addspace "abcdef"

This User Gave Thanks to Chubler_XL For This Post:
# 4  
Old 03-20-2011
Thanks! Actually, the expansion didn't work correctly when trying offsets but I found a way around with expr substr. All 3 would probably work with a little changing for my particular need, but this was the easiest so it's what I ended up with.

Code:
LEN=${#1}
count=1
    while [ "$count" -lt "$LEN" ]
    do
       echo -n "$(expr substr $1 $count 1 ) "
       count=$(($count + 1))
    done
echo

# 5  
Old 03-20-2011
Can I suggest a slight change - to fix last character not output and to support spaces within string:

Code:
LEN=${#1}
count=1
while [ "$count" -lt "$LEN" ]
do
    echo -n "$(expr substr "$1" $count 1 ) "
    count=$(($count + 1))
done
echo "$(expr substr "$1" $count 1 )"

# 6  
Old 03-22-2011
Quote:
Originally Posted by Chubler_XL
Can I suggest a slight change - to fix last character not output and to support spaces within string:
Thanks. After I posted and was "finalizing" the script I noticed the missing last letter, which I solved by adding 1 to the length:
Code:
LEN=$((${#1} + 1))

I also added the quotes out of habit. I didn't see how they would matter at first since it's taking the first argument. If the argument is abc def, it will see it as abc whether it's quoted or not. But now I see that quoting both the argument on the command line and variable in the script is the only way it will take an argument with spaces using $1.

Luckily I was using
Code:
foo "$(can infile)"

and it won't contain any spaces anyway, but that's good to know.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed]: syntax to insert N spaces in front of a string

Dear all, I would like to insert N blankspaces in front of a string using sed command To give an example (N=10), I tried that code: $ echo "abcd" | sed 's/^/ \{10,\}&/' but I failed, by obtaining that result: {10,}abcd Any help would be greatly appreciated, Thanks in advance,... (18 Replies)
Discussion started by: dae
18 Replies

2. Shell Programming and Scripting

Pure POSIX shell scripting...

Hi all... This is more of a concensus question than help... As many of you know I am experimenting with the limitations of Pure POSIX shell scripting. Q: Is the directory /bin considered part of the Pure POSIX shell or must I stick entirely with the builtins only? The reason is I... (2 Replies)
Discussion started by: wisecracker
2 Replies

3. UNIX for Dummies Questions & Answers

insert multiple characters in string

Hello, newb here :o How do I add square brackets before and after the first character in a string using sed? e.g. 0123456 123456 My attempts have been fruitless. sed 's/.\{0\}//' Thanks. (2 Replies)
Discussion started by: shadyuk
2 Replies

4. Shell Programming and Scripting

Korn shell to insert cyrillic characters into the databse

i have written a shell script that reads a csv file and inserts tokenized strings into the database. the problem comes when the csv file has cyrillic characters. how do i set the parameters in my shell script(korn shell) so that any characters can be inserted into the database. (3 Replies)
Discussion started by: vkca
3 Replies

5. Shell Programming and Scripting

Insert varying length spaces between words

Hey all, Fist post, so be kind... I have written an expect script which logs into a terminal and gathers several screens of information. Unfortunately the log file gives me all the special escape and control characters from the terminal. I am hoping to use a combination of shell scripting, sed,... (1 Reply)
Discussion started by: mpacer
1 Replies

6. Shell Programming and Scripting

Insert space between characters using sed

Input: Youcaneasilydothisbyhighlightingyourcode. Putting space after three characters. You can eas ily dot his byh igh lig hti ngy our cod e. How can i do this using sed? (10 Replies)
Discussion started by: cola
10 Replies

7. Shell Programming and Scripting

Insert a special character $ in place of extra spaces

Hi Experts, I have called some.txt with the following content. oracle HYRDSRVIHUB01 pts/0 TESTIHUB 07-JUN-10 CREATE TABLE TESTIHUB PHONE ... (12 Replies)
Discussion started by: naree
12 Replies

8. Shell Programming and Scripting

How to insert greek characters in to vi editor

Hi, I want to test a unix file by inserting greek characters in to vi editor. Can anyone please suggest how to insert greek characters in to vi editor. (2 Replies)
Discussion started by: DSDexter
2 Replies

9. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: heyindy06
4 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question