2017-04-03 76 views
1

特定行,我需要在我zshrc文件使用bash腳本添加新的插件,這樣做,我搜索包含插件=(sometext)行搜索和替換使用bash

語法

plugin_text=$(grep "^[^#;]" zshrc | grep -n "plugins=(.*)") 

直接在終端上運行我得到:

$ grep "^[^;]" zshrc | grep -n "plugins=(.*)" 
38:# Example format: plugins=(rails git textmate ruby lighthouse) 
40:plugins=(git python pip) 

40是正確的路線但瓦特母雞我執行我的bash腳本我得到:

$ ./config-minimal 
3:plugins=(git python pip) 

我需要更改40行插入新的插件。例如:

plugins=(git python pip) 

plugins=(git python pip zsh-autosuggestions zsh-syntax-highlighting) 

後,我怎樣才能得到這條線,並用簡單的方式替換文本

過嗎?

我的腳本

function install_zsh { 
    # aptitude install zsh 
    # sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)" 
    # Install zsh highlighting 
    # cd ~/.oh-my-zsh/custom 
    # git clone https://github.com/zsh-users/zsh-syntax-highlighting.git 
    # Install zsh auto suggestions 
    # git clone git://github.com/zsh-users/zsh-autosuggestions 
    # TODO: Add options in plugins 
    cd ~ 
    plugin_text=$(grep "^[^#;]" .zshrc | grep -n "plugins=(.*)") 
    new_plugins=${plugin_text/)/ zsh-autosuggestions zsh-syntax-highlighting)} 
    line_number=${plugin_text/:plugins*/ } 
    sed "$(line_number)s/plugin_text/new_plugins/" .zshrc 
} 
+0

你不需要假設該行是唯一的行號。 – 123

回答

1

你可以用一個簡單的sed處理:

sed 's/^plugins=(\(.*\)/plugins=(zsh-autosuggestions zsh-syntax-highlighting \1/' .zshrc 

或(THX @ 123):

sed 's/\(^plugins=([^)]*\)/\1 zsh-autosuggestions zsh-syntax-highlighting/' .zshrc 

添加-i標誌INFILE更換。


function install_zsh { 
    # aptitude install zsh 
    # sh -c "$(wget https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh -O -)" 
    # Install zsh highlighting 
    # cd ~/.oh-my-zsh/custom 
    # git clone https://github.com/zsh-users/zsh-syntax-highlighting.git 
    # Install zsh auto suggestions 
    # git clone git://github.com/zsh-users/zsh-autosuggestions 
    # TODO: Add options in plugins 

    sed -i.bak 's/^plugins=(\(.*\)/plugins=(zsh-autosuggestions zsh-syntax-highlighting \1/' ~/.zshrc 
} 
+1

也可以這樣做'sed's/\(^ plugins =([^)] * \)/ \ 1 zsh-autosuggestions zsh-syntax-highlighting /' – 123

+0

這就對了@ 123 – klashxx