使用Sed计算字符串中每个单词的字符数

时间:2015-10-25 12:13:44

标签: unix sed

所以我的问题是你如何计算Linux中给定字符串中每个单词的个别字符?

以下是我想要实现的目标:

$ echo "But a time I spent wandering in bloomy night;" | ...

应该给我:

314159265

3代表单词'But',1代表'a'等等......

1 个答案:

答案 0 :(得分:1)

不确定为什么指定sed;数不胜数。但awk很容易实现这一点:

$ echo "But a time I spent wandering in bloomy night" |
  awk '{for (i=1;i<=NF;++i) printf "%d", length($i); print ""}' 
314159265

请注意,您的版本中的分号将被计算,这意味着它会错误地将最后一位数字设为6而不是5.如果您只想计算字母,可以将sed重新引入混合:< / p>

$ echo "But a time I spent wandering in bloomy night;" |
  sed 's/[^a-zA-Z ]//g' |
  awk '{for (i=1;i<=NF;++i) printf "%d", length($i); print ""}'

或者,您可以使用shell内置函数完成所有操作。假设你的shell是bash,这将起作用:

echo "But a time I spent wandering in bloomy night;" | {
read -a pi   
for d in "${pi[@]}"; do
  d=${d//[^A-Za-z]}
  echo -n ${#d}
done
echo; }

从AWK走另一条路,它是Perl或Ruby中的一个单行程序:

$ echo "But a time I spent wandering in bloomy night;" |
  perl -lne 'print map { s/[^A-Za-z]//g; length } split' 

$ echo "But a time I spent wandering in bloomy night;" |
  ruby -ne 'puts $_.split.map{|w| w.gsub(/[^A-Za-z]/, "").length }.join'