首页 > 解决方案 > 从文件中删除与另一个文件的空白行相对应的行

问题描述

我有两个具有相同行数和列数的文件。用 分隔;。例子;

文件_a:

1;1;1;1;1
2;2;2;2;2
3;3;3;3;3
4;4;4;4;4

文件_b:

A;A;A;A;A
B;B;;;B
;;;;
D;D;D;D;D

忽略分隔符,第 3 行从file_b. 所以我想file_a在命令之前删除第 3 行;

paste -d ';' file_a file_b.

为了有这样的输出:

1;1;1;1;1;A;A;A;A;A
2;2;2;2;2;B;B;;;B
4;4;4;4;4;D;D;D;D;D

编辑:每行和两个文件的列数为 93 并且相同,因此两个文件具有完全相同的行和列矩阵。

标签: awkpasteblank-line

解决方案


您能否尝试在 GNU 中使用显示的示例进行跟踪、编写和测试awk

awk '
BEGIN{
  FS=OFS=";"
}
FNR==NR{
  arr[FNR]=$0
  next
}
!/^;+$/{
  print arr[FNR],$0
}
' file_a file_b

说明:为上述添加详细说明。

awk '                 ##Starting awk program from here.
BEGIN{                ##Starting BEGIN section from here.
  FS=OFS=";"          ##Setting field separator and output field separator as ; here.
}
FNR==NR{              ##Checking condition if FNR==NR which will be TRUE when file_a is being read.
  arr[FNR]=$0         ##Creating arr with index FNR and value is current line.
  next                ##next will skip all further statements from here.
}
!/^;+$/{              ##Checking condition if line NOT starting from ; till end then do following.
  print arr[FNR],$0   ##Printing arr with index of FNR and current line.
}
' file_a file_b       ##Mentioning Input_file names here.

推荐阅读