bash - 如何将长文本附加到 bash 中的特定行?
问题描述
我有一个 bash 脚本,可以复制和粘贴一个 txt 文件(T1,T2..T100)一百次。我需要在该文本文件的 txt 文件的第 454 行附加特定的代码行。
我尝试使用 sed 和 awk 但它们似乎不能正常工作。
我试图做这样的事情,但没有奏效:
awk 'NR==454{print "try:
Mesh_1.ExportMED( r'/home/students/gbroilo/Desktop/Script/Mesh_1.med', 0, SMESH.MED_V2_2, 1, None ,1)
pass
except:
print 'ExportToMEDX() failed. Invalid file name?'"}1'
解决方案
这可能对您有用(GNU sed):
sed -i '454a\line one with a quote '\''\n'\''another'\''\nlast line' fileT*
另一种方式:
cat <<\!>fileInsert
line one with a quote '
'another'
last line
!
sed -i '454r fileInsert' fileT*
完后还有:
cat <<\! | sed -i '454r /dev/stdin' fileT*
line one with a quote '
'another'
last line
!
如果要将特定文本插入特定文件,请使用 GNU 并行,如下所示:
parallel -q sed -i '/454r fileInsert{}' fileT{} ::: {1..100}
推荐阅读
- android - 如何在 Android 上保存和比较 2 个指纹
- typescript - 如果主题超出范围时未取消订阅,订阅 rxjs 主题会导致内存泄漏吗?
- c++ - AVL 树中的旋转
- php - 如何测试发出http请求的方法?
- php - Android将图像发布到php服务器总是失败
- javascript - 计算自特定日期以来的年月
- r - 在 R 中合并列
- r - 使用 Lat 和 Long 将 UTC 时间转换为 R 中的本地时间
- firebase - 即使在验证电子邮件后,FirebaseUser isEmailVerified 也始终返回 false
- elasticsearch - 是否可以一次将多个事件作为事务发布到 ElasticSearch?那么如果一个发布失败,ES 数据库会回滚吗?