在一个脚本中连接Wget和Sed命令?

时间:2014-01-21 18:01:55

标签: bash shell sed

我使用3个命令(wget / sed /和tr / sort),它们都在命令行中工作,以生成最常用的单词列表。我按顺序使用命令,保存sed的输出以在tr / sort命令中使用。现在我需要毕业编写一个结合了这三个命令的脚本。所以,1)wget下载一个文件,我将其放入2)sed -e 's/<[^>]*>//g' wget-file.txt,并输出&gt;转到3)

cat sed-output.txt | tr -cs A-Za-z\'  '\n' | tr A-Z  a-z | sort | uniq -c | 
sort -k1,1nr -k2 | sed ${1:-100}q > words-list.txt

我知道使用正则表达式删除HTML标记的问题/争论,但这3个命令暂时适用于我。所以,谢谢你帮助把它拉到一起。

2 个答案:

答案 0 :(得分:2)

使用awk。

wget -O- http://down.load/file| awk '{ gsub(/<[^>]*>/,"")                # remove the content in label <>
       $0=tolower($0)                    # convert all to lowercase
       gsub(/[^a-z]]*/," ")              # remove all non-letter chars and replaced by space
       for (i=1;i<=NF;i++) a[$i]++       # save each word in array a, and sum it.
     }END{for (i in a) print a[i],i|"sort -nr|head -100"}'   # print the result, sort it, and get the top 100 records only

答案 1 :(得分:0)

此命令应该完成这项工作:

wget -O- http://down.load/file | sed -e 's/<[^>]*>//g' | \
tr -cs A-Za-z\'  '\n' | tr A-Z  a-z | sort | uniq -c | \
sort -k1,1nr -k2 | sed ${1:-100}q > words-list.txt
相关问题