我有一个包含机场代码的文件,如下所示
ATQ
HYD
BLR
GOI
AMD
CCJ
TRV
从同一个文件我必须在gawk中创建如下所示的源目标映射。
src dest
ATQ HYD
ATQ BLR
ATQ GOI
ATQ AMD
ATQ CCJ
ATQ TRV
HYD ATQ
HYD BLR
HYD AMD
....
...
任何人都可以在这里帮助我,提前谢谢
答案 0 :(得分:1)
一个简单的awk
脚本可以做到这一点。只需将该行的所有元素存储在数组中,并为每个其他元素的每个元素运行一个循环,而无需重复,这些都没有意义。
$ 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
将输出,
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
答案 1 :(得分:1)
一个班轮:
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
将输出:
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
...
答案 2 :(得分:0)
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