How to separate tokens in line using Unix? [duplicate]

≡放荡痞女 提交于 2019-12-11 08:26:00

问题


How to separate tokens in line using Unix?

[in]:

some sentences are like this.
some sentences foo bar that

[out:]

some
sentences
are
like
this.

some
sentences
foo
bar
that

I could have done this in python as below, but is there any unix way to achieve the same output?

>>> import codecs
>>> outfile = codecs.open('outfile.txt','w','utf8')
>>> intext = "some sentences are like this.\n some sentences foo bar that"
>>> infile = codecs.open('infile.txt','w','utf8')
>>> print>>infile, intext
>>> for i in codecs.open('infile.txt','r','utf8'):
...     for j in i.split():
...             print>>outfile, j
...     print>>outfile
... 
>>> exit()

alvas@ubi:~$ cat outfile.txt 
some
sentences
are
like
this.

some
sentences
foo
bar
that

回答1:


Using sed:

$ cat infile.txt
some sentences are like this.
some sentences foo bar that
$ sed 's/\s\+\|$/\n/g' infile.txt > outfile.txt
$ cat outfile.txt
some
sentences
are
like
this.

some
sentences
foo
bar
that



回答2:


Using xargs

xargs -n1 < file



回答3:


sed -e 's/ \|$/\n/g' < text

should do?



来源:https://stackoverflow.com/questions/21778582/how-to-separate-tokens-in-line-using-unix

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!