tags:

views:

81

answers:

3

I need to print the following sequence for illustration purposes in two columns

a-z

which has alphabets from a to z such that they are in 13-character columns.

How can you echo the characters from a to z into two columns?

+2  A: 

Better solutions exist, I'm sure, but I'll give it a shot:

$ echo "abcdefghijklmnopqrstuvwxyz" | sed -e 's/\(.\)\(.\)/\1 \2\n/g'
a b
c d
e f
g h
i j
k l
m n
o p
q r
s t
u v
w x
y z
Stephan202
@Stephan: Thank you for your answer! I had a similar solution in mind. Regexes understand the command a-z. I still believe that there is a built-in way to expand the a-z.
Masi
I get the following output for your command: a bnc dne fng hni jnk lnm nno pnq rns tnu vnw xny zn. I uses Mac Leopard's sed.
Masi
@Masi, i suspect you missed a '\'... did you type? for i in {a..z}; do echo -n $i; done | sed -e 's/\(.\)\(.\)/\1 \2n/g'
nik
@Masi, Try changing 's/\(.\)\(.\)/\1 \2n/g' to 's/\(.\)\(.\)/\1 \2\n/g'
nik
@nik: I did not use the for-loop. --- No difference for your last code.
Masi
+2  A: 

Very nice Stephan,

How about avoiding to type a through z with a loop?

for i in {a..z}; do echo -n $i; done | sed -e 's/\(.\)\(.\)/\1 \2\n/g'
nik
+1. I wasn't aware of the {a..z} notation :)
Stephan202
I get the following output: { an. .nz }n
Masi
Masi, either there are c/p issues, or you have a weird shell :P
Stephan202
@Stephan, I use the for loop on unix shell extensively and end up avoiding many short scripts -- the history (CTRL+R) works like a great script cache!
nik
@Stephan: I get the following in Bash: a bnc dne fng hni jnk lnm nno pnq rns tnu vnw xny zn
Masi
@Stephan: I use Mac Leopard.
Masi
A: 

Your question did not specify how to distribute the characters in the two coloumns, so here is an alternative answer:

prompt> paste <(echo "abcdefghijklm" | sed 's/\(.\)/\1\n/g' ) <(echo "nopqrstuvwxyz" | sed 's/\(.\)/\1\n/g')
a       n
b       o
c       p
d       q
e       r
f       s
g       t
h       u
i       v
j       w
k       x
l       y
m       z

prompt>
hlovdal
The command does not work in OS/X Leopard and Zsh.
Masi