Bash 在shell脚本中,将file1中的一行粘贴到file2中的特定行之后

Bash 在shell脚本中,将file1中的一行粘贴到file2中的特定行之后,bash,shell,Bash,Shell,我想使用shell脚本将一个文件中的一行粘贴到第二个文件中的特定行 我有两个文件-file1和file2。我想粘贴file1中的第5行,使其在file2中显示为第7行,并且file2中的所有行也显示在输出中。当然,有多种方法可以做到这一点 awk NR为超记录数;脚本的第一行将file1的第5行保存在变量line5中。FNR!=读取第二个文件时,NR条件适用;它打印该行,如果第二个文件中的行号为6,它还打印line5 sed 这将提供给第二个(外部)sed,它将file1中的第5行添加到file

我想使用shell脚本将一个文件中的一行粘贴到第二个文件中的特定行


我有两个文件-
file1
file2
。我想粘贴
file1
中的第5行,使其在
file2
中显示为第7行,并且
file2
中的所有行也显示在输出中。

当然,有多种方法可以做到这一点

awk
NR为超记录数;脚本的第一行将
file1
的第5行保存在变量
line5
中。
FNR!=读取第二个文件时,NR
条件适用;它打印该行,如果第二个文件中的行号为6,它还打印
line5

sed
这将提供给第二个(外部)
sed
,它将
file1
中的第5行添加到
file2
的第6行之后

file1
file2
输出
欢迎来到堆栈溢出。请尽快阅读这一页。如果我推断您要复制
file1
的第5行,因此它显示为
file2
的第7行,我是否理解正确,文件2的第1-6行和第7-EOF行也出现在输出中?第1-6行+然后第一个文件的第5行,然后文件2的第7-EOF行也出现在输出中@Jonathan Lefflertank u如此之多….m将相同的工作捆绑起来非常感谢您
awk 'NR == 5 { line5 = $0 }
     FNR != NR { print; if (FNR == 6) print line5 }' file1 file2
sed -f <(sed -e '1,4d; 6,$d; x; s/.*/6a\\/;p; x' file1) file2
6a\
All important line 5
Garbage line 1
Garbage line 2
Garbage line 3
Garbage line 4
All important line 5
Garbage line 6
Garbage line 7
Garbage line 8
Garbage line 9
This is line 1 in file2
This is line 2 in file2
This is line 3 in file2
This is line 4 in file2
This is line 5 in file2
This is line 6 in file2
This is line 7 in file2
This is line 8 in file2
This is line 9 in file2
This is line 1 in file2
This is line 2 in file2
This is line 3 in file2
This is line 4 in file2
This is line 5 in file2
This is line 6 in file2
All important line 5
This is line 7 in file2
This is line 8 in file2
This is line 9 in file2