假设我的文件test.txt
包含内容:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam rhoncus dictum eros, eu molestie lectus cursus sit amet. Aliquam lectus erat, ultricies sit amet venenatis et, tincidunt eget magna.
我需要的是,逐字读取整个文件,但是......按字词排序。所以:
Aliquam
Aliquam
consectertur
dictum
...
任何想法?
答案 0 :(得分:8)
tr ' ' '\n' < test.txt | sort -u
如果您需要重复项,请删除-u
答案 1 :(得分:2)
for w in `cat test.txt`; do echo "$w"; done | sort
删除标点符号:
for w in `cat test.txt`; do echo "$w"; done | sed 's,[^a-zA-Z],,g' | sort
答案 2 :(得分:0)
tr "[[:space:]]" "\n" < file| sed 's/[[:punct:]]$//' | sort
红宝石(1.9 +)
ruby -0777 -ne 'puts $_.split.map{ |x| x.gsub(/[[:punct:]]$/,"") }.sort' file