awk - 从文件中删除与另一个文件的空白行相对应的行

标签 awk paste blank-line

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

1;1;1;1;1
2;2;2;2;2
3;3;3;3;3
4;4;4;4;4
file_b:
A;A;A;A;A
B;B;;;B
;;;;
D;D;D;D;D
忽略定界符,file_b中的第3行为空。因此,我也想在命令之前从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,并且每行和两个文件的列数相同,因此两个文件的行和列矩阵完全相同。

最佳答案

您能否请尝试按照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.

关于awk - 从文件中删除与另一个文件的空白行相对应的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64041626/

相关文章:

perl - 需要将 file2 中与 file1 第一列匹配的字符串替换为 file1 第二列

linux - 奇怪的 awk 输出包含空格

sorting - 如何使用 awk 按长度对行进行排序?

c# - Wpf MVVM 如何处理 ViewModel 中的 TextBox "paste event"

windows - 批处理 : Search for string to skip lines above and write results to new file

linux - 从文本文件 shell 脚本中删除 "empty"行

bash - 在 awk 中使用多个分隔符

linux - 运行带有长参数的 linux 命令时出现文件名太长错误

python - 如何使用 Python 将每个 Excel 工作表中的数据复制并粘贴到最终工作表中?

python - 在python中打印多个空行