gpt4 book ai didi

shell - 如何从标记开始将文件的内容附加到另一个文件中?

转载 作者:行者123 更新时间:2023-12-04 06:29:49 25 4
gpt4 key购买 nike

例如,file1 包含:

Line 1
This is another line a
and another
<BEGIN>

few more lines.

文件 2 包含:
/* This is a line With Special Characters */
/* Another line with @ special stuff \ ? # ! ~ */
/* and another */

我想在 < BEGIN > 语句之后的位置将 file2 插入到 file1 中。

我尝试了以下 sed 命令,但它似乎将 '/' 和 '*' 视为特殊字符。
TOINSERT=`cat file2`
sed "/BEGIN/ a $TOINSERT" file1 > output_file

但是,我收到一个错误,因为 $TOINSERT 包含特殊字符。有没有办法转义 $TOINSERT 的所有内容?

最佳答案

#!/bin/bash
sed '/<BEGIN>/ {
r file2
d
}' < file1 > output_file

注:如果您想与 <BEGIN> 保持一致只需使用:
 sed '/<BEGIN>/r file2' < file1 > output_file

概念证明
$ ./insertf.sh
Line 1
This is another line a
and another
/* This is a line With Special Characters */
/* Another line with @ special stuff \ ? # ! ~ */
/* and another */

few more lines.

关于shell - 如何从标记开始将文件的内容附加到另一个文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5559717/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com