Merge two files in linux with different column

后端 未结 4 698
野趣味
野趣味 2020-12-28 08:58

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 an

相关标签:
4条回答
  • 2020-12-28 09:06

    Not sure which columns you want from each file, but something like this should work:

    paste <file1> <file2> | awk '{print $1,$2,$3,$5}'
    

    The first three columns would be picked from file1, and the fourth skipped, then pick the first column from the second file.

    0 讨论(0)
  • 2020-12-28 09:11

    If the files have the same number of rows, you can do something like:

    awk '{ getline v < "file2"; split( v, a ); print a[2], $1, $3 }' file1
    

    to print colums 1 and 3 from file 1 and column 2 from file2.

    0 讨论(0)
  • 2020-12-28 09:17
    paste file1 file2 | awk '{print $1,$2,$3,$5}'
    
    0 讨论(0)
  • 2020-12-28 09:19
    you can try this one without paste command:
    awk '{print $1}{print $2}{print $3}' file1 >> mergedfile
    awk '{print $2}' file2 >> mergedfile
    
    0 讨论(0)
提交回复
热议问题