Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a new line to a large file every n characters in terminal (one liner sed)?

What am I missing here?

file.txt:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

in Terminal:

> sed "s/.\{3\}/&\n/g" < file.txt > new-file.txt

result: new-file.txt

ABCnDEFnGHInJKLnMNOnPQRnSTUnVWXnYZ

Expected Result:

ABC
DEF
...
VWX
YZ
like image 744
Ryan Avatar asked Dec 06 '22 16:12

Ryan


2 Answers

Use sed:

$ sed 's/.../&\n/g' file.txt

Or use grep:

$ grep -oE '.{1,3}' file.txt

result:

ABC
DEF
GHI
JKL
MNO
PQR
STU
VWX
YZ
like image 153
kev Avatar answered Jan 05 '23 01:01

kev


$ echo abcdefghi | dd cbs=3 conv=unblock 2>/dev/null 
abc
def
ghi
like image 22
James Youngman Avatar answered Jan 05 '23 01:01

James Youngman