简体   繁体   中英

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. Spotify does not use the Login Items or launchd to start and therefore I had to figure this out.

Spotify has a file called prefs located in ~/Library/Application Support/Spotify . I can edit the file manually by adding the line app.autostart-mode="off" after the line core.clock_delta=0 in the file. 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

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 . One suggestion I read was to add a -i to awk but that is not a recognized option. 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.

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.

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.

The -e option may be necessary for macOS.

The " do not need to be escaped as they have no significance in the a command.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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