当脚本包含来自行的公共字符串时,行会被截断

当脚本包含来自行的公共字符串时,行会被截断

我有这个脚本

BEGIN {
  OFS=FS=";"
}
ARGIND == 1 {
   map[$2] = ($2 in map ? map[$2] OFS : "") $1
   next
}
{
   r = $0;
   for (i=1; i<=NF; ++i)
      if ($i in map)
         r = r OFS map[$i]
   $0 = r
}
1

和这个

#! /usr/bin/awk  -f

BEGIN {
  OFS=FS=";"
  C=0;
}
{
  if (ARGIND == 1) {
     X = $NF
     T0[$NF] = C++
     $NF = ""
     if (T1[X]) {
        T1[X] = T1[X] $0
     } else {
        T1[X] = $0
     }
  } else {
     X = $NF
     T0[$NF] = C++
     $NF = ""
     if (T2[X]) {
        T2[X] = T2[X] $0
     } else {
        T2[X] = $0
     }
  }
}

END {
  for (X in T0) {
    # concatenate T1[X] and X, since T1[X] ends with ";"
    print T1[X]  X, T2[X]
  }
}

我是这样使用的

awk -f join2.awk g2.txt g1.txt

如果 g1.txt 和 g2.txt 是这样的,那么这个脚本就可以正常工作

文本文件

alfa beta;www.google.com
Light Dweller - CR, Technical Metal;http://alfa.org;http://beta.org;http://gamma.org;

文本文件

Jack to ride.zip;http://alfa.org;
JKr.rui.rar;http://gamma.org;
Nofj ogk.png;http://gamma.org;

它正确生成此输出 g3.txt

alfa beta;www.google.com
Light Dweller - CR, Technical Metal;http://alfa.org;http://beta.org;http://gamma.org;;Jack to ride.zip;JKr.rui.rar;Nofj ogk.png

但是如果我的 g1.txt 和 g2.txt 是这样写的,那么同样的脚本会截断字符串,并且会出现问题

文本文件

alfa beta;https://123.org/a/in.ph?shw=297585;
Light Dweller;https://123.org/a/in.ph?shw=3869;
Imp - Alp;https://123.org/a/in.ph?shw=340249;

文本文件

Link;1996 7.zip;https://yd.k/d/BbNUb-2yhVmVnw;https://123.org/a/in.ph?shw=3869;https://123.org/a/in.ph?shw=3869&st=30;
Link;Moonblo (Tape)_123.zip;https://yd.k/d/57i;https://123.org/a/in.ph?shw=3869&st=30;
Link;Moonblo(Demo) [1996].rar;https://yd.k/d/A7U;https://123.org/a/in.ph?shw=3869;

它给了我这个错误的输出

alfa beta;https://123.org/a/in.ph?shw=297585;;;
Light Dweller;https://123.org/a/in.ph?shw=3869;;;
Imp - Alp;https://123.org/a/in.ph?shw=340249;;;

但我想要这个正确的输出

alfa beta;https://123.org/a/in.ph?shw=297585;
Light Dweller;https://123.org/a/in.ph?shw=3869;Link;1996 7.zip;https://yd.k/d/BbNUb-2yhVmVnw;https://123.org/a/in.ph?shw=3869&st=30;Link;Moonblo(Demo) [1996].rar;https://yd.k/d/A7U;
Imp - Alp;https://123.org/a/in.ph?shw=340249;

为什么是这个输出?
因为https://123.org/a/in.ph?shw=3869它必须将 g2.txt 中的整行放在 g1.txt 文本文件中的行上,即 ==> g1.txt 行 ==>Light Dweller;https://123.org/a/in.ph?shw=3869

相关内容