文件中的空字符串如何回车换行?


比如文件中有这样的字符串:

a.txt


 Rei9aiwe bohth4Zu Go3eimum iChieSh5
iveeSh2J eiziV0bo lu2Efooz feey5Ohr

要转换成下面的形势:

b.txt


 Rei9aiwe
bohth4Zu
Go3eimum
iChieSh5
iveeSh2J
eiziV0bo
lu2Efooz
feey5Ohr

如何做?

语言不限,PHP/Ruby/Python都可以。

ruby python php

wichan 11 years, 2 months ago

 sed 's/\s\+/\n/g' a.txt > b.txt

或者


 tr ' ' '\n' < a.txt > b.txt

或者用 Python:


 python -c 'open("b.txt", "w").write(open("a.txt").read().replace(" ", "\n"))'

这样的任务,超过一行的代码就实在是太浪费啦。

Sharl answered 11 years, 2 months ago

Your Answer