老男孩教育每日一题-第84天-两个文件,把第一个文件中的第2、3行内容添加到第二个文件的第3行后面
2017-07-04 21:46
417 查看
两个文件如下:
往期题目索引http://lidao.blog.51cto.com/3388056/1914205
[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
相关文章推荐
- 一个模式字符串,一个替换用字符串,以及两个文件名 它应该读取第一个文件, 并将内容写入第二个文件
- 老男孩教育每日一题-2017年3月16日: linux网络配置中如何给一块网卡添加多个IP地址
- 老男孩教育每日一题-2017年5月4日-有一个oldboy.txt文件,把里面所有字母都转换成大写
- 老男孩教育每日一题-第125天-显示文件oldboy.txt的第20行到30行请问如何做?
- 老男孩教育每日一题-2017年4月10日-find查找到文件并复制系列题目
- 老男孩教育每日一题-第89天-根据如下描述使用正则表达式取出内容
- 封装一下 javaMail,帐号密码配置在配置文件中,要发送的内容 和收件人列表后面再添加
- 老男孩教育每日一题-2017-04-21-命令风暴--查看你的Linux系统的文件系统类型?
- 老男孩教育每日一题-第78天-如何不显示文件中的空行?
- 读取两个压缩包,比较文件夹及文件内容的不同之处并添加到另一个文件夹打包下载
- iTextSharp之pdfRead(两个文件文本内容的比较,指定页数的pdf截取,水印的添加)
- shell如何在指定文件的指定位置后面添加内容
- 老男孩教育每日一题-2017年5月3日-写一个脚本:创建一个函数,能接受两个参数:
- 老男孩教育每日一题-第62天-统计/var/log/下所有文件个数
- 老男孩教育每日一题-第111天-linux中一个文件由哪几部分组成?
- 老男孩教育每日一题-第109天-linux下创建各种类型的文件
- 老男孩教育每日一题-第102天-如何找出/usr/local 下面所有shell脚本文件,并设置执行权限?
- 老男孩教育每日一题-第61天-使用命令调换 /etc/passwd 文件里所有的第一列和最后一列位置
- 老男孩教育每日一题-2017年5月16日-说说{}与[]这两个符号有什么区别?
- 老男孩教育每日一题-2017-04-18:命令风暴:如何快速删除Linux中海量小文件?