AWK动力装置实施 [英] AWK power set implementation

查看:101
本文介绍了AWK动力装置实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本页提供了电源设置的实现,这是我的看法:

This page provides a power set implementation in shell, and here is my take on it:

pa() {
  if [ "$#" = 0 ]
  then echo
  else (
    shift
    pa "$@"
  ) | while read qu
    do printf '%s %s\n%s\n' "$1" "$qu" "$qu"
    done
  fi
}
pa x y z

我认为上一页的作者发表这一评论很有趣:

I thought it was interesting that the author of the above page made this comment:

没有好的AWK解决方案.欢迎您给我发一封电子邮件:<他的电子邮件>

no nice AWK solution. You are welcome to email me one: <his email>

这不能在AWK中完成吗,或者Shell只是在这里做得更好?

Can this not be done in AWK, or does the shell just do a better job here?

推荐答案

这是另一种AWK方法:

Here is another AWK approach:

echo a b c | awk '{for(i=0;i<2^NF;i++) {
                     for(j=0;j<NF;j++)
                        if(and(i,(2^j))) printf "%s ",$(j+1)
                     print ""}}'

a
b
a b
c
a c
b c
a b c

如果您的AWK不具有and()功能,则将其替换为int(i/(2^j))%2.

If your AWK doesn't have the and() function, replace it with int(i/(2^j))%2.

这篇关于AWK动力装置实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆