在Bourne shell中按换行符和空格拆分字符串

时间:2010-05-27 15:55:56

标签: shell sh

我目前正在使用以下内容将文件拆分为单词 - 有更快的方法吗?

while read -r line
do
    for word in $line
    do
        words="${words}\n${word}"
    done
done

3 个答案:

答案 0 :(得分:9)

如何使用tr

tr -s '[:space:]' '\n' < myfile.txt

-s将多个空格字符压缩为一个。

答案 1 :(得分:2)

xargs -n 1  echo <myfile.txt

答案 2 :(得分:2)

sed 's/[[:space:]]/\n/g' file.txt