![如果单词以特定字符串结尾,如何添加字符串](https://linux22.com/image/133828/%E5%A6%82%E6%9E%9C%E5%8D%95%E8%AF%8D%E4%BB%A5%E7%89%B9%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BB%93%E5%B0%BE%EF%BC%8C%E5%A6%82%E4%BD%95%E6%B7%BB%E5%8A%A0%E5%AD%97%E7%AC%A6%E4%B8%B2.png)
我们要更新以下文件,以防 MACHINE_NAME 结尾“.localdomain”然后需要在单词的开头添加字符串 linux
每个 MACHINE_NAME 都带有单括号“......”
更改前的文件示例
['server_primary01.localdomain', 'server_primary02.localdomain', 'server_primary03.localdomain']
EDIT_HOSTS.append( {'HOST' : 'Client0{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'Client{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'Client0{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'Client{0}.localdomain'.format(count+1)} )
更改后的示例
['linuxserver_primary01.localdomain', 'linuxserver_primary02.localdomain', 'linuxserver_primary03.localdomain']
EDIT_HOSTS.append( {'HOST' : 'linuxClient0{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'linuxClient{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'linuxClient0{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'linuxClient{0}.localdomain'.format(count+1)} )
答案1
sed
解决方案:
sed -E "s/[^[:space:]']+\.localdomain/linux&/g" file
示例输出:
['linuxserver_primary01.localdomain', 'linuxserver_primary02.localdomain', 'linuxserver_primary03.localdomain']
EDIT_HOSTS.append( {'HOST' : 'linuxClient0{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'linuxClient{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'linuxClient0{0}.localdomain'.format(count+1)} )
EDIT_HOSTS.append( {'HOST' : 'linuxClient{0}.localdomain'.format(count+1)} )