您的位置:首页 > 其它

老男孩教育每日一题-第84天-两个文件,把第一个文件中的第2、3行内容添加到第二个文件的第3行后面

2017-07-04 21:46 417 查看
两个文件如下:
[root@oldboy ~]# cat 1.txt
111
222
333
[root@oldboy ~]# cat 2.txt AAA
bbb
ccc
ddd
要求修改后的文件
[root@oldboy ~]# cat 2.txt
AAA
bbb
ccc
222
333
ddd
``

参考答案:

方法1:

[root@oldboy ~]# sed -n 2,3p 1.txt |xargs |sed -r 's# #\\n#g'|sed -r 's#.*#sed -i "3a&" 2.txt#g' |bash
[root@oldboy ~]# cat 2.txt
AAA
bbb
ccc
222
333
ddd

方法2:

[root@oldboy36 ~]# sed -i "3a$(sed -n '2,3p' 1.txt |xargs |sed 's# #\\n#g')" 2.txt
[root@oldboy36 ~]# cat 2.txt
AAA
bbb
ccc
222
333
ddd

方法3:

[root@oldboy36 ~]# awk 'BEGIN{while("cat 1.txt"|getline){a++;if(a>=2&&a<=3){b=b"\n"$0}};while("cat 2.txt"|getline){c++;if(c==3){print $0,b > "2.txt"}else{print $0 > "2.txt"}}close("2.txt")}'
[root@oldboy36 ~]# cat 2.txt
AAA
bbb
ccc
222
333
ddd

备注

今天是每日一题陪伴大家的第84天期待你的进步。对于题目和答案的任何疑问,请在博客评论区留言
往期题目索引http://lidao.blog.51cto.com/3388056/1914205
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  每日一题
相关文章推荐