2011-11-29 96 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手冊。