简体   繁体   English

使用awk或shell脚本或python将文件中单个列的所有组合创建为两列

[英]Create all combinations of a single column from a file into two columns using awk or shell script or python

I have a file which contains airport code like below 我有一个包含机场代码的文件,如下所示

ATQ 
HYD 
BLR
GOI 
AMD
CCJ
TRV

from the same file i have to create source destination mapping like below in gawk. 从同一文件中,我必须在gawk中创建如下所示的源目标映射。

 src dest
 ATQ HYD
 ATQ BLR
 ATQ GOI
 ATQ AMD
 ATQ CCJ
 ATQ TRV
 HYD ATQ
 HYD BLR
 HYD AMD
 ....
 ...

can anyone help me here,thanks in advance 任何人都可以在这里帮助我,谢谢

A simple awk script could do this. 一个简单的awk脚本可以做到这一点。 Just store all the elements of the line in array and run a loop for each element for every other element without repetitions which don't make sense. 只需将行中的所有元素存储在数组中,然后为每个其他元素的每个元素运行一个循环,而不必重复那些没有意义的操作。

$ awk 'BEGIN {printf("%s\t%s\n" ,"src", "dest")}
  {
      line[++c] = $1
  }
  END {
      for ( i = 1; i <= c; i++ )
      {
          for ( j = 1; j <= c; j++ )
          {
              if (line[i] != line[j]) { printf("%s\t%s\n",line[i],line[j]) }
          }
       }
  }
' file

will output, 将输出

src     dest
ATQ     HYD
ATQ     BLR
ATQ     GOI
ATQ     AMD
ATQ     CCJ
ATQ     TRV
HYD     ATQ
HYD     BLR
HYD     GOI
HYD     AMD
HYD     CCJ
HYD     TRV
BLR     ATQ
BLR     HYD
BLR     GOI
BLR     AMD
BLR     CCJ
BLR     TRV
GOI     ATQ
GOI     HYD
GOI     BLR
GOI     AMD
GOI     CCJ
GOI     TRV
AMD     ATQ
AMD     HYD
AMD     BLR
AMD     GOI
AMD     CCJ
AMD     TRV
CCJ     ATQ
CCJ     HYD
CCJ     BLR
CCJ     GOI
CCJ     AMD
CCJ     TRV
TRV     ATQ
TRV     HYD
TRV     BLR
TRV     GOI
TRV     AMD
TRV     CCJ

One liner: 一班轮:

 echo "src dest"; cat /tmp/air | while read s; do for d in `cat /tmp/air`; do if [ $s != $d ]; then echo $s $d; fi; done; done

Will output: 将输出:

src dest
ATQ HYD
ATQ BLR
ATQ GOI
ATQ AMD
ATQ CCJ
ATQ TRV
HYD ATQ
HYD BLR
HYD GOI
HYD AMD
HYD CCJ
HYD TRV
BLR ATQ
BLR HYD
BLR GOI
BLR AMD
BLR CCJ
BLR TRV
GOI ATQ
...

Version in bash assuming your airport list is in a.txt file: bash中的版本(假设您的机场列表位于a.txt文件中):

echo "src dest";
for src in `cat a.txt`; do
    for dest in `cat a.txt`; do
        if [ $src != $dest ]; then
            echo "$src $dest";
        fi
    done;
done

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从两个数据框创建所有列组合的子图 - How to create subplots of all column combinations from two dataframes 如何获得两列的所有组合? (蟒蛇) - How to get the all the combinations of two columns? (Python) 在Python中创建两组列表的所有组合 - Create all combinations of two sets of lists in Python Python:计算从csv文件导入的两列的差,并存储到python脚本中的另一列 - Python : Calculate the difference of two columns imported from a csv file and store to another column in python script 如何使用 python 脚本将 excel 中的多列复制到单列 - How to copy multiple columns to single column in excel using python script 如何创建包含所有可能组合和聚合结果的两列的 groupby - How to create a groupby of two columns with all possible combinations and aggregated results Shell脚本中的sed,awk或python - sed , awk or python in shell script Openpyxl &amp; Python - 将两列单元格中的数据合并为一列 - Openpyxl & Python - Combining data from cells in two columns into a single column Python:在 dataframe 中创建包含列表作为其值的两列的组合 - Python: create combinations of two columns containing lists as their value in a dataframe 从两个现有数据框创建两个新数据框,同时考虑其多索引列及其值的所有组合 - Create two new data frames from two existing data frames, taking into account all combinations of their multi-index columns and their values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM