binbjz
binbjz

Reputation: 881

AWK: convert string to a column

I want to convert string (eg: abcdef) to a column

This is what I want.

a
b
c
d
e
f

I know how to covert string to column by using sed

$ echo abcdef | sed 's/[^.]/&\n/g'|sed '$d'

But how to covert it using awk?

Upvotes: 2

Views: 732

Answers (3)

Akshay Hegde
Akshay Hegde

Reputation: 16997

[akshay@localhost tmp]$ awk -v ORS= 'gsub(/./,"&\n")' <<<"abcdefgh"
a
b
c
d
e
f
g
h

Upvotes: 3

fedorqui
fedorqui

Reputation: 290525

You can set the field separator to an empty string, so that every character is a different field. Then, loop through them and print:

$ awk -v FS="" '{for (i=1;i<=NF;i++) print $i}' <<< "abcdef"
a
b
c
d
e
f

Which is equivalent to:

awk -F "" '{for (i=1;i<=NF;i++) print $i}' <<< "abcdef"

Upvotes: 2

chaos
chaos

Reputation: 9332

Only working with awks internal variables:

echo abcdef | awk 'BEGIN{FS="";OFS="\n"}{$1=$1}1'

It sets the input field separator (FS) to nothing which means that every character is a field. The output field separator (OFS) is set to newline. Notive the $1=$1 is needed to rebuild the record with the new OFS.

Upvotes: 0

Related Questions