Extract words from files

亡梦爱人 提交于 2019-12-11 01:09:03

问题


How can I extract all the words from a file, every word on a single line? Example:

test.txt

This is my sample text

Output:

This
is
my
sample
text

回答1:


The tr command can do this...

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

This asks the tr program to replace white space with a new line. The output is stdout, but it could be redirected to another file, result.txt:

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



回答2:


And here the obvious bash line:

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

EDIT Still shorter:

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

That's all there is to it, no special (pathetic) cases included (And handling multiple subsequent word separators / leading / trailing separators is by Doing The Right Thing (TM)). You can adjust the notion of a word separator using the $IFS variable, see bash manual.




回答3:


The above answer doesn't handle multiple spaces and such very well. An alternative would be

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

which would. E.g.

esben@mosegris:~/ange/linova/build master $ echo "test    test" | tr [:blank:] '\n' 
test



test

But

esben@mosegris:~/ange/linova/build master $ echo "test    test" | perl -p -e '$_ = join("\n",split);' 
test
test



回答4:


This might work for you:

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



回答5:


perl answer will be :

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> 


来源:https://stackoverflow.com/questions/8318316/extract-words-from-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!