2011-11-29 92 views
2

如何从文件中提取所有单词,单个单词上的每个单词? 实施例:从文件中提取单词

的test.txt

This is my sample text 

输出:

This 
is 
my 
sample 
text 

回答

5

tr命令可以做到这一点...

tr [:blank:] '\n' < test.txt 

此询问tr程序来代替白色有新线的空间。 输出为标准输出,但它可能会被重定向到另一个文件,的Result.txt:

tr [:blank:] '\n' <test.txt> result.txt 
+0

@Chistopher一个小小的诡辩 - 你可能想要添加'-s'来挤压白色空间。 – potong

0

以上回答不处理多个空格和这样的非常好。替代方案是

perl -p -e '$_ = join("\n",split);' test.txt 

哪会。例如。

[email protected]:~/ange/linova/build master $ echo "test test" | tr [:blank:] '\n' 
test 



test 

[email protected]:~/ange/linova/build master $ echo "test test" | perl -p -e '$_ = join("\n",split);' 
test 
test 
0

这可能会为你工作:

# echo -e "this  is\tmy\nsample text" | sed 's/\s\+/\n/g'   
this 
is 
my 
sample 
text 
0

Perl的答案是:

pearl.214> cat file1 
a b c d e f pearl.215> perl -p -e 's/ /\n/g' file1 
a 
b 
c 
d 
e 
f 
pearl.216> 
1

而这里明显的bash行:

for i in $(< test.txt) 
do 
    printf '%s\n' "$i" 
done 

编辑更短:

printf '%s\n' $(< test.txt) 

这一切就是这么简单,没有什么特别的(可怜)案件包括(和处理多个后续单词分隔/前/后分离是做正确的事(TM值))。您可以使用$ IFS变量调整字词分隔符的概念,请参阅bash手册。