在bash中的另一个文件中间附加一个文件 [英] Append a file in the middle of another file in bash

查看:73
本文介绍了在bash中的另一个文件中间附加一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在另一个文件的特定位置附加一个文件.我得到了行号,所以我的文件是:

I need to append a file in a specific location of another file. I got the line number so, my file is:

file1.txt :

I
am
Cookie

第二个是

file2.txt :

a
black
dog
named

因此,解决方案完成后, file1.txt 应该类似于

So, after the solution, file1.txt should be like

I
am
a
black
dog
named
Cookie

该解决方案应该与两个文件中都存在诸如",/之类的字符兼容.

The solution should be compatible with the presence of characters like " and / in both files.

只要是本地工具,任何工具都可以(我的意思是,无需安装新软件).

Any tool is ok as long as it's native (I mean, no new software installation).

推荐答案

能否请您关注并告诉我,这是否对您有帮助.

Could you please try following and let me know if this helps you.

awk 'FNR==3{system("cat file2.txt")} 1' file1.txt

输出如下.

I
am
a
black
dog
named
Cookie

说明::在此处读取名为file1.txt的Input_file时检查行号是否为3,如果是,则使用awk的系统实用程序,它将帮助我们调用Shell的命令,然后打印出file2.txt(使用cat命令).然后提到1将打印file1.txt中的所有行.因此,我们可以将file2.txt中的行连接到file1.txt中.

Explanation: Checking here if line number is 3 while reading Input_file named file1.txt, if yes then using system utility of awk which will help us to call shell's commands, then I am printing the file2.txt with use of cat command. Then mentioning 1 will be printing all the lines from file1.txt. Thus we could concatenate lines from file2.txt into file1.txt.

这篇关于在bash中的另一个文件中间附加一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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