【发布时间】:2013-10-02 03:22:26
【问题描述】:
我有一个文本文件 (~8 GB)。让我们将此文件称为 A。文件 A 大约有 100,000 行,其中 19 个单词和整数由空格分隔。我需要从文件 A 中剪切几行并将它们粘贴到一个新文件(文件 B)中。应从文件 A 中删除这些行。从文件 A 中删除的行应具有完全匹配的字符串。 然后我需要重复几次,每次从文件 A 中删除具有不同匹配字符串的行。每次,文件 A 都在变小。 我可以使用“sed”但使用两个命令来做到这一点,如下所示:
# Finding lines in file A with matching string and copying those lines to file B
sed -ne '/\<matchingString\>/ p' file A > file B
#Again finding the lines in file A with matching string and deleting those lines,
#writing a tmp file to hold the lines that were not deleted.
sed '/\<matchingString\>/d'file A > tmp
# Replacing file A with the tmp file.
mv tmp file A
这是文件 A 和 B 的示例。我想提取所有包含 hg15 的行 文件 A:
ID pos frac xp mf ...
23 43210 0.1 2 hg15...
...
...
File B:
23 43210 0.1 2 hg15...
我对编写 shell 脚本和使用所有 Unix 工具还很陌生,但我觉得我应该能够更优雅、更快地完成这件事。谁能指导我改进这个脚本。我不需要特别使用“sed”。我一直在搜索 web 和 stackoverflow,但没有找到解决这个确切问题的方法。我正在使用 RedHat 和 bash。 谢谢。
【问题讨论】:
-
你能发布一些输入和预期输出的示例数据吗?当我们不需要猜测时,让我们的生活变得如此轻松:-)