比如文件中有這樣的字符串:
a.txt
Rei9aiwe bohth4Zu Go3eimum iChieSh5
iveeSh2J eiziV0bo lu2Efooz feey5Ohr
要轉(zhuǎn)換成下面的形勢:
b.txt
Rei9aiwe
bohth4Zu
Go3eimum
iChieSh5
iveeSh2J
eiziV0bo
lu2Efooz
feey5Ohr
如何做?
語言不限,PHP/Ruby/Python都可以。
業(yè)精于勤,荒于嬉;行成于思,毀于隨。
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"))'
這樣的任務(wù),超過一行的代碼就實在是太浪費啦。
既然語言不限,來一個另類的,目測沒幾個知道是什么語言
Import["a.txt"]~StringReplace~(" " -> "\n") // Export["b.txt", #] &
補充個ruby 版
open("b.txt", "w").puts open('a.txt').read.gsub " ", "\n"
python
myfile = open("a.txt")
temp = ""
for line in myfile.readlines():
for a in line.split():
temp += a + '\n'
myfile.close()
file_object = open('b.txt', 'w')
file_object.writelines(temp)
file_object.close()