bash将列切成一个文件并保存到另一个文件的末尾 [英] bash cut columns to one file and save onto the end of another file

查看:60
本文介绍了bash将列切成一个文件并保存到另一个文件的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to cut two columns from one file and stick them on the end of a second file. The two file have the exact same number of lines

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

到目前为止,我一直在使用

So far I have been using

cut -f9-10 file2.txt  | paste file1.txt - > file3.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

但是我不想创建一个新文件,我希望将文件1更改为上面的文件.我已经尝试过

However I don't want to have to make a new file I would prefer to alter file 1 to the above. I've tried

cut -f9-10 file2.txt  | paste file1.txt -

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

but it simply prints everything on screen. Is there a way of just adding columns 9 and 10 to the end of file1.txt?

推荐答案

使用 moreutils !它使您可以吸收标准输入并写入文件.也就是说,要在管道之后就地替换文件.

Use sponge from moreutils! It allows you to soak up standard input and write to a file. That is, to replace a file in-place after a pipe.

cut -f9-10 file2.txt  | paste file1.txt - | sponge file1.txt

请注意,您也可以使用 paste

Note you can also do what you are doing by using paste with a process substitution.

$ 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

使用''作为分隔符,将 file1.txt file2.txt 中的最后两列连接起来.

This joins file1.txt with two last columns from file2.txt using ' ' as delimiter.

这篇关于bash将列切成一个文件并保存到另一个文件的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆