2014-02-14 49 views
1

如何使用Unix分離令牌?如何使用Unix分離令牌?

[中]:

some sentences are like this. 
some sentences foo bar that 

[出來:]

some 
sentences 
are 
like 
this. 

some 
sentences 
foo 
bar 
that 

我可以在Python如下這樣做,但有任何UNIX的方式來實現相同的輸出?

>>> 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() 

[email protected]:~$ cat outfile.txt 
some 
sentences 
are 
like 
this. 

some 
sentences 
foo 
bar 
that 
+0

也看到,http://stackoverflow.com/questions/21779272/reverse-newline-tokenization-in-one-token-per-line-files-unix?noredirect=1 #comment32949628_21779272 – alvas

回答

2

使用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 
+0

任何想法如何做相反? – alvas

+0

@alvas,請發佈一個單獨的問題。 – falsetru

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

應該怎麼辦?

1

使用xargs的

xargs -n1 < file