如何防止SED改变线路断路?

发布于 2025-02-08 10:17:56 字数 494 浏览 3 评论 0原文

我正在用.js文件更改.sh文件的内容,但是有不必要的副作用。

# update file
version="0.1"
file="Test.js"
updated_line="const version = \"$version\";"
sed -i "1s/.*/$updated_line/" $file

const version = "0";
const moreVars = {};

在离开线路破裂时,我该如何进行这些更改?

I am changing content of a .js file with a .sh file but there's an unwanted side-effect.

.sh file:

# update file
version="0.1"
file="Test.js"
updated_line="const version = \"$version\";"
sed -i "1s/.*/$updated_line/" $file

Test.js file:

const version = "0";
const moreVars = {};

After running the .sh file the update seems to be successfull but my IDE is complaining that the linebreaks changed from CRLF to LF:
Changed linebreaks

How can I make these changes while leaving the linebreaks the way they were?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜妞爱困 2025-02-15 10:17:56

sed并不是line break意识到,从这个意义上说,cr字符只是cr字符。如果您希望自己的行具有CR角色,则必须添加CR字符。对于SED,它只是一个像其他人一样的角色。

updated_line="const version = \"$version\";"

在离开线路破裂时,我该如何进行这些更改?

写一个正则态度,如果存在并保留线条末端的CR字符。

s/.*\(\x0d\?\)$/$updated_line\1/
\r'

在离开线路破裂时,我该如何进行这些更改?

写一个正则态度,如果存在并保留线条末端的CR字符。

sed is not linebreak aware, in that sense, CR character is just that CR character. If you want your line to have CR character, you have to add CR character. For sed it's just a character like any other.

updated_line="const version = \"$version\";"

How can I make these changes while leaving the linebreaks the way they were?

Write a regex that matches CR character on the end of the line if it exists and preserve it.

s/.*\(\x0d\?\)$/$updated_line\1/
\r'

How can I make these changes while leaving the linebreaks the way they were?

Write a regex that matches CR character on the end of the line if it exists and preserve it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文