Extract words from files

2019-07-11 20:59发布

问题:

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> 


标签: bash extract