Print every character in a new line

Saturday, December 29, 2012 , , 1 Comments


I have a file like this:

    This is a sentence.
    This is another sentence.

I need to put a new line after each character, such that only one character appears on every line, e.g.:


    T
    h
    i
    s
    
    i
    s
    
    a
    
    s
    e
    n
    t
    e
    n
    c
    e
    .
    T
    h
    i
    s
    
    i
    s
    
    a
    n
    o
    t
    h
    e
    r
    
    s
    e
    n
    t
    e
    n
    c
    e
    .

Solution for this is:
sed:
sed 's/\(.\)/\1\n/g' -i filename
perl:
perl -F// -lane 'print join "\n", @F' filename
awk:
awk -F '' -v 'OFS=\n' '{$1=$1}1' filename

1 comment:

  1. Thank you for sharing! Did you know that sed accepts any 's' delimiter? That means you can write it so it is easier to read:
    sed 's_\(.\)_\1\n_g' -i filename

    ReplyDelete