【问题标题】:How to separate tokens in line using Unix? [duplicate]如何使用 Unix 将标记分开? [复制]
【发布时间】:2014-02-14 20:25:15
【问题描述】:

如何使用 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()

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

some
sentences
foo
bar
that

【问题讨论】:

标签: file unix tokenize


【解决方案1】:

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

【讨论】:

  • 知道如何做相反的事情吗?
  • @alvas,请发表一个单独的问题。
【解决方案2】:

使用 xargs

xargs -n1 < file

【讨论】:

    【解决方案3】:
    sed -e 's/ \|$/\n/g' < text
    

    应该做吗?

    【讨论】:

      猜你喜欢
      • 2015-09-15
      • 2011-07-07
      • 2016-09-04
      • 2011-11-02
      • 2013-03-31
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      相关资源
      最近更新 更多