在特定位置将一个表的列添加到另一个表

时间:2018-09-15 14:35:56

标签: linux awk paste

我有一个表,各表之间用制表符分隔,还有一个从其他文件创建的列。表格如下:

表1:

        IDriveFile file = DriveClass.DriveApi.GetFile(GoogleInfo.GetInstance().SignInApi, driveID);
        file.GetMetadata(mGoogleApiClient).SetResultCallback(metadataRetrievedCallback());
        Task.Run(() =>
        {

            var driveContentsResult = file.Open(mGoogleApiClient,
                DriveFile.ModeReadOnly, null).Await();

            IDriveContents driveContents = driveContentsResult.JavaCast<IDriveApiDriveContentsResult>().DriveContents;

            Stream inputstream = driveContents.InputStream;

            byte[] buffer = new byte[16 * 1024];
            int read;
            MemoryStream output = new MemoryStream();

            while ((read = inputstream.Read(buffer, 0, buffer.Length)) > 0)
            {
                output.Write(buffer, 0, read);
            }

表2:

col1 col2 col3
 ch   NA   3
 ch   NA   4
 ch   NA   5

所需的输出:

colX
 AA
 AA
 AA

我知道 粘贴 可以将列添加到表的末尾或开头,但是,如何将列添加到其他表的任何位置我想要的?我想使用bash命令而不是R,因为文件很大,并且我不想将它们上传到R。

1 个答案:

答案 0 :(得分:0)

请您尝试以下。

awk 'FNR==NR{a[FNR]=$0;next} {$1=$1 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]=$0        ##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.
}
{
  $1=$1 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.