[Solved] How to separate one line to mutiple line based on certain number of characters?

hi Gurus,

I need separate a file which is one huge line to multiple lines based on certain number of charactors. for example:

abcdefghi high abaddffdd

I want to separate the line to multiple lines for every 4 charactors.
the result should be

abcd
efgh
i hi
gh a
badd
ffdd

Thanks in advance.

You can use fold command:

fold -w4 file
1 Like

Thank you very much!
you are great!!!

:b:

With awk

awk '{for (i=1;i<=length($0);i+=4) print substr($0,i,4)}' file
1 Like

Thanks, the code works perfect. Great man !!!

For completeness:

sed 's/.\{4\}/&\
/g' file

Yes, sed needs two lines. GNU sed also takes one line with \n .