AWK: convert row to column

I want to convert a string (ex:) abcdef

to a column

This is what I want.

a
b
c
d
e
f

      

I know how to hide a row in a column using sed

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

      

But how do you hide it with awk

?

+3


source to share


3 answers


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

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

      



Which is equivalent :

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

      

+2


source


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

      



+3


source


Works only with internal awks variables:

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

      

It sets the input fields delimiter ( FS

) to nothing, which means each character is a field. The output field separator ( OFS

) is set to a new line. Please note: $1=$1

it is necessary to restore a recording with a new one OFS

.

0


source







All Articles