在第n行替换文件中的字符串

Rav*_*dra 5 unix bash replace sed

只需要在第n行文件中替换文件中的字符串

文件1

  hi this is line 1
  hi this is line 2
  hi this is line 3
  hi this is line 4
Run Code Online (Sandbox Code Playgroud)

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

专家如下

  hi this is line 1
  Hello this is line 2
  hi this is line 3
  hi this is line 4
Run Code Online (Sandbox Code Playgroud)

我尝试创建一个临时文件

  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
Run Code Online (Sandbox Code Playgroud)

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

谢谢

pot*_*ong 11

这可能对你有用:

sed -i '2s/hi/Hello/' file
Run Code Online (Sandbox Code Playgroud)