bash 合并Linux中具有不同列的两个文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11160145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge two files in linux with different column
提问by rosas
I have two files in linux, the first file has 4 columns and the second has 2 columns. I want to merge these files into a new file that has the first 3 columns from file 1 and the first column from file 2. I tried awk, but my data from file 2 was placed under file 1.
我在 linux 中有两个文件,第一个文件有 4 列,第二个文件有 2 列。我想将这些文件合并到一个新文件中,该文件包含文件 1 的前 3 列和文件 2 的第一列。我尝试了 awk,但文件 2 中的数据放在文件 1 下。
回答by Prince John Wesley
paste file1 file2 | awk '{print ,,,}'
回答by twalberg
Not sure which columns you want from each file, but something like this should work:
不确定您想要从每个文件中获取哪些列,但这样的操作应该可行:
paste <file1> <file2> | awk '{print ,,,}'
The first three columns would be picked from file1
, and the fourth skipped, then pick the first column from the second file.
将从 中选取前三列file1
,跳过第四列,然后从第二个文件中选取第一列。
回答by William Pursell
If the files have the same number of rows, you can do something like:
如果文件的行数相同,您可以执行以下操作:
awk '{ getline v < "file2"; split( v, a ); print a[2], , }' file1
to print colums 1 and 3 from file 1 and column 2 from file2.
打印文件 1 中的第 1 列和第 3 列以及文件 2 中的第 2 列。
回答by gameboy90
you can try this one without paste command:
awk '{print }{print }{print }' file1 >> mergedfile
awk '{print }' file2 >> mergedfile