当前位置: 技术问答>linux和unix
如何利用Bash脚本(利用awk\sed\grep\wc等)来自动修改配置文件
来源: 互联网 发布时间:2017-01-04
本文导语: 现在有一个配置文件something.conf,内容是 default =0 dubug = 1,4,enable enable = 1,autostart dubug = 2,8,disable enable = 1,autostart dubug = 1,4,disable enable = 1,autostart dubug = 1,4,enable enable = 1,autostart 现在想写一个bash脚本,...
现在有一个配置文件something.conf,内容是
default =0
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
现在想写一个bash脚本,能够
1.根据default来判断需要修改哪组个配置
2.能在dubug或者enable后面自动加参数
请问能做到吗?
抱歉浪费大家脑细胞了!
default =0
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
现在想写一个bash脚本,能够
1.根据default来判断需要修改哪组个配置
2.能在dubug或者enable后面自动加参数
请问能做到吗?
抱歉浪费大家脑细胞了!
|
有点繁杂,抛砖引玉
使用时将需要加入的参数作为shell脚本参数传入即可
实例文本:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
命令:chgArg.sh helloooooooooooooooo
结果:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable,helloooooooooooooooo
enable = 1,autostart,helloooooooooooooooo
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
使用时将需要加入的参数作为shell脚本参数传入即可
#! /bin/sh
awk -F"=" -v arg=$1 'BEGIN{flag=0;group=0}{if($0 ~"default"){flag=$2;}if($0 ~"dubug")group++;if(flag==group && $0 ~"dubug"){$0=$0","arg;} if(flag==group && $0 ~"enable")$0=$0","arg;print $0}' yourfile
实例文本:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable
enable = 1,autostart
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart
命令:chgArg.sh helloooooooooooooooo
结果:
default =2
dubug = 1,4,enable
enable = 1,autostart
dubug = 2,8,disable,helloooooooooooooooo
enable = 1,autostart,helloooooooooooooooo
dubug = 1,4,disable
enable = 1,autostart
dubug = 1,4,enable
enable = 1,autostart