bash将列剪切为一个文件并保存到另一个文件的末尾

cat*_*cha 6 linux bash cut paste

我想从一个文件中剪切两列,并将它们粘贴在第二个文件的末尾.这两个文件具有完全相同的行数

file1.txt
1  2  3  4  5  6  7  8  9  10  
1  2  3  4  5  6  7  8  9  10
1  2  3  4  5  6  7  8  9  10

file2.txt
a  b  c  d  e  f  g  h i  j
a  b  c  d  e  f  g  h i  j
a  b  c  d  e  f  g  h i  j
a  b  c  d  e  f  g  h i  j
Run Code Online (Sandbox Code Playgroud)

到目前为止我一直在使用

cut -f9-10 file2.txt  | paste file1.txt - > file3.txt
Run Code Online (Sandbox Code Playgroud)

它输出的正是我想要的

1  2  3  4  5  6  7  8  9  10  i  j
1  2  3  4  5  6  7  8  9  10  i  j
1  2  3  4  5  6  7  8  9  10  i  j
Run Code Online (Sandbox Code Playgroud)

但是我不想创建一个新文件,我宁愿将文件1更改为上面的文件.我试过了

cut -f9-10 file2.txt  | paste file1.txt -
Run Code Online (Sandbox Code Playgroud)

但它只是在屏幕上打印所有内容.有没有办法只将第9列和第10列添加到file1.txt的末尾?

fed*_*qui 2

使用spongemoreutils !它允许您soak up standard input and write to a file。也就是说,在管道之后就地替换文件。

cut -f9-10 file2.txt  | paste file1.txt - | sponge file1.txt
Run Code Online (Sandbox Code Playgroud)

paste请注意,您还可以通过使用进程替换来完成您正在做的事情。

$ paste -d' ' file1.txt <(awk '{print $(NF-1), $NF}' file2.txt) | sponge file1.txt
$ cat file1.txt
1  2  3  4  5  6  7  8  9  10 i j
1  2  3  4  5  6  7  8  9  10 i j
1  2  3  4  5  6  7  8  9  10 i j
Run Code Online (Sandbox Code Playgroud)

这与用作分隔符的file1.txt最后两列连接。file2.txt' '