用其他文件的前几行替换前几行 [英] Replace first few lines with first few lines from other file
本文介绍了用其他文件的前几行替换前几行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在Linux上工作.我有2个文件-file1.dat和file2.dat.
I am working on Linux. I have 2 files - file1.dat and file2.dat.
cat file1.dat
1
2
3
4
5
6
7
8
9
10
和文件2:
cat file2.dat
1a
2a
3a
4a
5a
6a
7a
8a
9a
10a
我想用file2.dat的前3行替换file1.dat的前4行.所以我的输出将是
I want to replace first 4 lines from file1.dat with first 3 lines from file2.dat. So my output would be following
cat file1.dat
1a
2a
3a
5
6
7
8
9
10
我尝试了以下输入:
sed -i.bak '1,4d;3r file2.dat' file1.dat
但是使用此输入,我有以下输出:
But with this input I have following output:
5
6
7
8
9
10
如何修改输入命令?我尝试了各种组合.
How should I modify input command? I tried various combinations.
推荐答案
awk
是你的朋友
脚本
# awk 'NR==FNR && FNR<=3 || NR>FNR && FNR>4' file2 file1
输出
1a
2a
3a
5
6
7
8
9
10
提示
- NR-已处理的记录总数
- FNR-已处理的记录总数,但在读取新文件时会重置.
- 当条件评估为true并且没有给出任何额外的命令时,仅打印
awk
.
所有良好的:-)
这篇关于用其他文件的前几行替换前几行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文