简体   繁体   English

在文件中添加新行

[英]Adding a new line to a file

I am trying to use a bash script to disable the ability for Spotify to start at login on macOS.我正在尝试使用 bash 脚本来禁用 Spotify 在 macOS 上登录时启动的功能。 Spotify does not use the Login Items or launchd to start and therefore I had to figure this out. Spotify 不使用登录项或启动项来启动,因此我必须弄清楚这一点。

Spotify has a file called prefs located in ~/Library/Application Support/Spotify . Spotify 在~/Library/Application Support/Spotify中有一个名为prefs的文件。 I can edit the file manually by adding the line app.autostart-mode="off" after the line core.clock_delta=0 in the file.我可以通过在文件中的core.clock_delta=0行之后添加行app.autostart-mode="off"手动编辑文件。 What I am trying to figure out is how to do this with a script.我想弄清楚的是如何用脚本来做到这一点。 I have tried the following:我尝试了以下方法:

sed '/core.clock_delta=0/a app.autostart-mode=\"off\"' prefs

This gives me the error sed: 1: "/core.clock_delta=0/a a...": command a expects \ followed by text这给了我错误sed: 1: "/core.clock_delta=0/a a...": command a expects \ followed by text

awk '1;/core.clock_delta=0/{ print "app.autostart-mode=\"off\""; }' prefs

This outputs the text of the prefs file the way I expect it to be with the new line but doesn't write anything to the prefs file, which makes sense considering it is awk .这会以我期望的方式输出prefs文件的文本,但不会向prefs文件写入任何内容,考虑到它是awk ,这是有道理的。 One suggestion I read was to add a -i to awk but that is not a recognized option.我读到的一个建议是在awk中添加-i ,但这不是一个公认的选项。 I cannot use the line after where I need this as this line can change with updates.我不能在需要它的地方使用该行,因为该行可能会随着更新而改变。 I also cannot just replace the entire file with a new one as it also contains hashed account information.我也不能只用一个新文件替换整个文件,因为它还包含散列帐户信息。 I also tried adding this line to the end of the file but then Spotify will crash on first run and then replace the file.我也尝试将此行添加到文件末尾,但 Spotify 会在第一次运行时崩溃,然后替换文件。

Basically what I am looking for is for this基本上我正在寻找的是这个

storage.last-location="~/Library/Application Support/Spotify/PersistentCache/Storage"
core.clock_delta=0
app.last-launched-version="1.1.55.498.gf9a83c60"
app.autostart-configured=true

To become this成为这个

storage.last-location="~/Library/Application Support/Spotify/PersistentCache/Storage"
core.clock_delta=0
app.autostart-mode="off"
app.last-launched-version="1.1.55.498.gf9a83c60"
app.autostart-configured=true

I have searched for and tried solutions on here but none of them seem to work, and some people's suggestions were 'I did this...' with no explanation of how they did 'this.'我在这里搜索并尝试了解决方案,但似乎没有一个有效,有些人的建议是“我做了这个……”,但没有解释他们是如何做到“这个”的。

With your shown samples, could you please try following.使用您显示的示例,您能否尝试以下操作。 Simply find the match of string and print current line and value you want to print.只需找到字符串的匹配项并打印您要打印的当前行和值。

awk '/core\.clock_delta=0/{print $0 ORS "app.autostart-mode=\"off\"";next} 1' Input_file

OR one could do:或者可以这样做:

awk '/core\.clock_delta=0/{$0=$0 ORS "app.autostart-mode=\"off\""} 1' Input_file

Once you are happy with results printed on screen/terminal, then append > temp && mv temp Input_file to above command to save changes into Input_file itself.一旦您对屏幕/终端上打印的结果感到满意,然后 append > temp && mv temp Input_file到上述命令以将更改保存到 Input_file 本身。

This might work for you:这可能对您有用:

sed -i'' -e '/core.clock_delta=0/a app.autostart-mode="off"' prefs

The -i option allows the changes to overwrite the original file. -i选项允许更改覆盖原始文件。

The -e option may be necessary for macOS. macOS 可能需要-e选项。

The " do not need to be escaped as they have no significance in the a command. "不需要转义,因为它们在a命令中没有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM