加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 将第n行中的字符串替换为文件

发布时间:2020-12-15 22:15:03 所属栏目:安全 来源:网络整理
导读:您只需要在第n行文件中替换文件中的字符串 文件1 hi this is line 1 hi this is line 2 hi this is line 3 hi this is line 4 我需要在第2行中替换’hi’ 专家如下 hi this is line 1 Hello this is line 2 hi this is line 3 hi this is line 4 我尝试创建
您只需要在第n行文件中替换文件中的字符串

文件1

hi this is line 1
  hi this is line 2
  hi this is line 3
  hi this is line 4

我需要在第2行中替换’hi’

专家如下

hi this is line 1
  Hello this is line 2
  hi this is line 3
  hi this is line 4

我尝试创建一个临时文件

sed -n 2p  file1 > temp1
  perl -pi -e 's/hi/Hello/g' temp1  I tried to replace temp1 with line 2 in file1
  sed -i  '2d' file1   after this I failed to insert temp1 as a 2nd line in file1

帮我替换第N行中的文件中的字符串(没有临时文件是首选..).

谢谢

解决方法

这可能对你有用:

sed -i '2s/hi/Hello/' file

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读