awk - 从文件中删除与另一个文件的空白行相对应的行
问题描述
我有两个具有相同行数和列数的文件。用 分隔;
。例子;
文件_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 并且相同,因此两个文件具有完全相同的行和列矩阵。
解决方案
您能否尝试在 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.
推荐阅读
- excel - Run macro on change within target range | Change (ByVal Target As Range)
- java - 如何解决SqlIntegrityConstraintViolationException?
- google-cloud-platform - Whoami doesn't change in GCP after I run gcloud auth login
- java - Spring MVC 中如何使用 IoC 和 DI
- ios - self.perform segue causes view to not conform
- c++ - 复制数组时出现随机垃圾
- android - 为什么房间数据库创建只需要同步 `Room.databaseBuilder.build`?
- django - 模板页面上的反向查找不起作用
- php - PHP Session 不能立即工作,但在刷新页面后工作
- python - 将什么作为 post_save 接收者的“发送者”?