sed 替换第一行中的字符串 [英] sed replace string in a first line

查看:127
本文介绍了sed 替换第一行中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用程序sed"替换文件的第一行中的字符串?

How can I replace a string but only in the first line of the file using the program "sed"?

命令 s/test/blah/11s/test/blah/ 似乎不起作用.还有别的方法吗?

The commands s/test/blah/1 and 1s/test/blah/ don't seem to work. Is there another way?

推荐答案

这可能对你有用(GNU sed):

This might work for you (GNU sed):

sed -i '1!b;s/test/blah/' file

只会在第一行用第一个 test 替换 blah.

will only substitute the first test for blah on the first line only.

或者如果您只想更改第一行:

Or if you just want to change the first line:

sed -i '1c\replacement' file 

这篇关于sed 替换第一行中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆