在特定位置从一个 table 添加一列到另一列
add a column from one table to another in a specific place
我有一个 table,由制表符分隔,一列是我从另一个文件创建的。 table 看起来像这样:
Table 1:
col1 col2 col3
ch NA 3
ch NA 4
ch NA 5
Table 2:
colX
AA
AA
AA
期望的输出:
col1 colX col2 col3
ch AA NA 3
ch AA NA 4
ch AA NA 5
我知道 paste 可以将列添加到我的 table 的末尾或开头,但是,如何我可以在我想要的其他 table 的任何位置添加该列吗?我想使用 bash 命令而不是 R,因为文件很大,我不想将它们上传到 R。
能否请您尝试以下。
awk 'FNR==NR{a[FNR]=[=10=];next} {= OFS a[FNR]} 1' table2 table1
输出如下。
col1 colX col2 col3
ch AA NA 3
ch AA NA 4
ch AA NA 5
说明:现在也补充说明。
awk '
FNR==NR{ ##FNR==NR is condition which will be TRUE when first Input_file named table2 is being read.
a[FNR]=[=12=] ##Creating an array named a wohse index is FNR and value is current line value.
next ##next will skip all following statements which be ONLY read when 2nd Input_file named table1 is being read.
}
{
= OFS a[FNR] ##Re-creating first field where it concatenates its own value with array a value whose index is FNR.
}
1 ##mentioning 1 will print edited or non-edited value of current line of table1 Input_file.
' table2 table1 ##Mentioning Input_file names here.
我有一个 table,由制表符分隔,一列是我从另一个文件创建的。 table 看起来像这样:
Table 1:
col1 col2 col3
ch NA 3
ch NA 4
ch NA 5
Table 2:
colX
AA
AA
AA
期望的输出:
col1 colX col2 col3
ch AA NA 3
ch AA NA 4
ch AA NA 5
我知道 paste 可以将列添加到我的 table 的末尾或开头,但是,如何我可以在我想要的其他 table 的任何位置添加该列吗?我想使用 bash 命令而不是 R,因为文件很大,我不想将它们上传到 R。
能否请您尝试以下。
awk 'FNR==NR{a[FNR]=[=10=];next} {= OFS a[FNR]} 1' table2 table1
输出如下。
col1 colX col2 col3
ch AA NA 3
ch AA NA 4
ch AA NA 5
说明:现在也补充说明。
awk '
FNR==NR{ ##FNR==NR is condition which will be TRUE when first Input_file named table2 is being read.
a[FNR]=[=12=] ##Creating an array named a wohse index is FNR and value is current line value.
next ##next will skip all following statements which be ONLY read when 2nd Input_file named table1 is being read.
}
{
= OFS a[FNR] ##Re-creating first field where it concatenates its own value with array a value whose index is FNR.
}
1 ##mentioning 1 will print edited or non-edited value of current line of table1 Input_file.
' table2 table1 ##Mentioning Input_file names here.