perl - 使用 sed/awk/perl 获取列列表
问题描述
我有不同的文件,如下格式
方案 1:
File1
no,name
1,aaa
20,bbb
File2
no,name,address
5,aaa,ghi
7,ccc,mn
我想获得列数更多的列列表,如果它的顺序相同
**Expected output for scenario 1 :**
no,name,address
方案 2:
File1
no,name
1,aaa
20,bbb
File2
no,age,name,address
5,2,aaa,ghi
7,3,ccc,mn
预期成绩 :
Both file headers and positions are different as a message
我对使用 bash / perl / sed / awk 的任何简短解决方案感兴趣。
解决方案
Perl 解决方案:
perl -lne 'push @lines, $_;
close ARGV;
next if @lines < 2;
@lines = sort { length $a <=> length $b } @lines;
if (0 == index "$lines[1],", $lines[0]) {
print $lines[1];
} else {
print "Both file headers and positions are different";
}' -- File1 File2
推荐阅读
- docker - Visual Studio 2017 Docker 编辑并继续 Razor 页面
- vba - 使用 Application.Match() 查找 Date()
- sql-server - 在 Oracle 数据库上创建指向 MS SQL Server 的数据库链接
- asp.net-mvc-5 - 出现错误 UserSession.OnActionExecuting(ActionExecutingContext): 在 mvc 5 中找不到合适的方法来覆盖?
- c# - “新相机相机”这个词在 Unity3d 中有什么作用?
- excel - 如何使用 office-js Excel API 的预览版?
- angular - HttpErrorResponse: api url jasmine test 的 Http 失败响应
- php - 仅当最后一个“-”之后的字符串是数字时,如何获取最后一个“-”之前的字符?
- android - 使用高斯模糊渲染系统壁纸
- swift4 - 如何使用 EurekaForm Library / Swift 在列表中获取所选值并设置默认值