【问题标题】:Split file contents to different files [duplicate]将文件内容拆分为不同的文件[重复]
【发布时间】:2020-01-21 17:10:03
【问题描述】:

我有一个这样的示例文件。

this is a sample_file for testing single_words and
multiple_words from
file

我想要在不同文件中包含 _ 的单个单词和单词。

singlewords.txt

this
is
a
for
testing
and
from
file

multiwords.txt

sample_file
single_words
multiple_words

输入文本文件大约 30GB。执行此操作的最佳方法是什么?

从评论中添加:

试过egrep -o '\b(\w*_\w+)\b' words.txt > multiwords.txt。但不确定如何将剩余单词写入下一个文件

【问题讨论】:

  • 脏但它应该可以工作:sed -e "s/ /\n/g" your_file | grep -v "_" > singlewords.txtsed -e "s/ /\n/g" your_file | grep "_" > multiwords.txt 如果你的文件太大,你可以使用 split 命令分割成几个块
  • @oguzismail 试过 egrep -o '\b(\w*_\w+)\b' words.txt > multiwords.txt。但不确定如何将剩余单词写入下一个文件
  • grep 有 -v 标志,因为它只会输出不匹配的行
  • @Jotne 我们不要开始编辑大战

标签: bash unix awk sed grep


【解决方案1】:

应该这样做:

awk '{for(i=1;i<=NF;i++) print $i > ($i~/_/?"multiwords.txt":"singlewords.txt")}' file

结果

cat multiwords.txt
sample_file
single_words
multiple_words
cat singlewords.txt
this
is
a
for
testing
and
from
file

不确定它处理 30GB 文件的能力如何,但可以测试一下。

【讨论】:

  • @oguzismail 这我明白,但无需从我添加到帖子中的评论中删除他测试的内容以供其他人查找。
猜你喜欢
  • 2017-07-15
  • 2014-08-14
  • 1970-01-01
  • 2015-11-14
  • 2022-10-15
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多