快速 shell 命令删除文本文件中的停用词

Fast shell command to remove stop words in a text file

我有一个 2GB 的文本文件。我正在尝试从此文件中删除频繁出现的英文停用词。

我有 stopwords.txt 包含这样的..

a
an
the
for
and
I

使用 shell 命令(例如 tr、sed 或 awk)执行此操作的快速方法是什么?

这是一个使用命令行的方法,perl:

将下面的文本保存为replacesw.sh:

#! /bin/bash
MYREGEX=\b\(`perl -pe 's/\n/|/g' `\)\b
perl -pe "s/$MYREGEX//g" 

然后,如果您已将上面的文件保存为 stopwords.txt,并且有一个名为 testtext.txt 的第二个文件(例如),其中包含:

This is a file with the stopwords from the stopwords.txt for testing.
More than one line in the file, for a better test.

然后命令行中的以下命令将删除 stopwords:

KBs-MBP13:temp kbenoit$ ./replacesw.sh stopwords.txt testtext.txt 
This is  file with  stopwords from  stopwords.txt  testing.
More than one line in  file,   better test.

您可能需要先 chmod u+x replacesw.sh