bash:所有行的组合

时间:2017-08-23 10:00:40

标签: bash awk

我有以下文件(这是分号分隔;真实文件是制表符分隔的)

abc;173959;172730
def;4186657;4187943
ghi;4703911;4702577
jkl;2243551;2242259

我希望将每一行与每一行合并,以便我的输出为:

abc;173959;172730;def;4186657;4187943
abc;173959;172730;ghi;4703911;4702577
abc;173959;172730;jkl;2243551;2242259
def;4186657;4187943;ghi;4703911;4702577
def;4186657;4187943;jkl;2243551;2242259
ghi;4703911;4702577;jkl;2243551;2242259

订单并不重要。

我提出了以下awk解决方案:

awk '{ a[$0] } END { for (i in a){ for (j in a){if (i != j)  print (i "\t" j) } } }' file

但这会在两个方向上打印出组合,例如

abc;173959;172730;def;4186657;4187943
def;4186657;4187943;abc;173959;172730

因为我对python或perl非常不熟悉,所以我请求使用awk / bash等解决方案。

3 个答案:

答案 0 :(得分:1)

在awk中:

$ awk '{ a[$0] }
END {
    for(i in a) {
        delete a[i]            # new place for delete
        for(j in a)
            if(i!=j)
                print i ";" j
        # delete a[i]          # previous and maybe wrong place
    }
}' file
def;4186657;4187943;ghi;4703911;4702577
def;4186657;4187943;abc;173959;172730
def;4186657;4187943;jkl;2243551;2242259
ghi;4703911;4702577;abc;173959;172730
ghi;4703911;4702577;jkl;2243551;2242259
abc;173959;172730;jkl;2243551;2242259

不幸的是,订单是随机的。

恢复订单的另一种方法是在处理(见注释)时修改a

$ awk '{ a[NR]=$0 }                  # index on NR
    END {
        for(i=1;i<=NR;i++)
            for(j=i+1;j<=NR;j++)     # j=i+1 is the magic
                print a[i] ";" a[j]
}' file
abc;173959;172730;def;4186657;4187943
abc;173959;172730;ghi;4703911;4702577
abc;173959;172730;jkl;2243551;2242259
def;4186657;4187943;ghi;4703911;4702577
def;4186657;4187943;jkl;2243551;2242259
ghi;4703911;4702577;jkl;2243551;2242259

答案 1 :(得分:1)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <div class="form-group"> <div class="col-sm-8 col-sm-offset-4 col-md-offset-4" onclick="myFunction()"> <input type="submit" value="Register" class="btn btn-primary btn-flat w-min-120" data-toggle="modal" data-target="#myModal"/> </div> <input type="hidden" id="myInput" value="<c:out value='${random_number}'/>"> <script> //When the user clicks on div, open the popup function myFunction() { //var str = VerificationCode.RandomNumber(); alert("myInput"); } </script> </div>也应该有效:

awk

答案 2 :(得分:1)

请你试试下面一个,它只会给你一个与Input_file的字段值相同的顺序,只读一次Input_file。

import { Directive, ElementRef, Input, Renderer2 } from '@angular/core';

@Directive ({
  selector: '[fbDecimalFormat]'
})

export class DecimalFormatDirective {

  constructor(private el: ElementRef,
              private renderer: Renderer2) {
    //renderer.setAttribute(el, 'value', parseFloat(el.nativeElement.value).toFixed(2));
    this.el.nativeElement.value = parseFloat(this.el.nativeElement.value).toFixed(2);
  }

}

awk '{a[FNR]=$0} END{j=1;while(length(a)>=++k){for(q=j+1;q<=FNR;q++){print a[j]";"a[q]}j++};}'  Input_file

输出如下。

awk  '
{
  a[FNR]=$0
}
END{
  j=1;
  while(length(a)>=++k){
     for(q=j+1;q<=FNR;q++){
       print a[j]";"a[q]
}
  j++
}
}
'   Input_file