sh/bash/dash/ksh/zsh等Shell脚本
-
Points
- 帖子: 83
- 注册时间: 2010-02-19 16:13
#1
帖子
由 Points » 2012-05-17 21:43
请教高手如何用脚本批量修改同目录多个文件中的指定内容??
原始:
代码: 全选
[connection]
id=xxx
uuid=xxx
type=802-11-wireless
[ipv4]
method=auto
要修改成:
代码: 全选
[connection]
id=xxx
uuid=xxx
type=802-11-wireless
autoconnect=false
[ipv4]
method=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=true
同目录有多个不同名的文本内容都有相关项目
小白求代码。。

-
枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
-
联系:
#2
帖子
由 枫叶饭团 » 2012-05-17 21:50
你是说增加autoconnect=false和dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;这两行吗?
-
Points
- 帖子: 83
- 注册时间: 2010-02-19 16:13
#4
帖子
由 Points » 2012-05-17 23:03
枫叶饭团 写了:你是说增加autoconnect=false和dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;这两行吗?
还有
代码: 全选
ignore-auto-dns=true
加三行总共是。。
-
Points
- 帖子: 83
- 注册时间: 2010-02-19 16:13
#5
帖子
由 Points » 2012-05-17 23:05
niejieqiang 写了:perl -ni -e 'print xxx if yy'
Sent from my U8800Pro using Tapatalk
兄台能写完整吗,看不懂或。。

-
枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
-
联系:
#6
帖子
由 枫叶饭团 » 2012-05-17 23:18
[python]
#/usr/bin/env python3
import fileinput
for i in fileinput.input(inplace=1):
i = i.rstrip()
print(i)
if i=="[connection]":
print("autoconnect=false")
if i=="[ipv4]":
print("dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;\nignore-auto-dns=true")
[/python]
效果将就把,顺序乱的没问题吧
代码: 全选
^_^[maplebeats@acer-5750G:~/Works/Scripts]$ cat test
[connection]
id=xxx
uuid=xxx
type=802-11-wireless
[ipv4]
method=auto
^_^[maplebeats@acer-5750G:~/Works/Scripts]$ python3 addword.py test
^_^[maplebeats@acer-5750G:~/Works/Scripts]$ cat test
[connection]
autoconnect=false
id=xxx
uuid=xxx
type=802-11-wireless
[ipv4]
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=true
method=auto
^_^[maplebeats@acer-5750G:~/Works/Scripts]$
-
Points
- 帖子: 83
- 注册时间: 2010-02-19 16:13
#7
帖子
由 Points » 2012-05-17 23:32
枫叶饭团 写了:[python]
#/usr/bin/env python3
import fileinput
for i in fileinput.input(inplace=1):
i = i.rstrip()
print(i)
if i=="[connection]":
print("autoconnect=false")
if i=="[ipv4]":
print("dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;\nignore-auto-dns=true")
[/python]
效果将就把,顺序乱的没问题吧
代码: 全选
^_^[maplebeats@acer-5750G:~/Works/Scripts]$ cat test
[connection]
id=xxx
uuid=xxx
type=802-11-wireless
[ipv4]
method=auto
^_^[maplebeats@acer-5750G:~/Works/Scripts]$ python3 addword.py test
^_^[maplebeats@acer-5750G:~/Works/Scripts]$ cat test
[connection]
autoconnect=false
id=xxx
uuid=xxx
type=802-11-wireless
[ipv4]
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=true
method=auto
^_^[maplebeats@acer-5750G:~/Works/Scripts]$
顺序不能乱哦,加加油。。改下。。

-
枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
-
联系:
#8
帖子
由 枫叶饭团 » 2012-05-17 23:38
[python]
#/usr/bin/env python3
import fileinput
for i in fileinput.input(inplace=1):
i = i.rstrip()
print(i)
if i=="uuid=xxx":
print("type=802-11-wireless")
if i=="method=auto":
print("dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;\nignore-auto-dns=true")
[/python]
这样子就行了,其实这脚本很sb。。
-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#9
帖子
由 YeLee » 2012-05-17 23:38
代码: 全选
sed -e 's/\(type=802-11-wireless\)$/\1\nautoconnect=false/g' -e 's/\(method=auto\)/\1\ndns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;\nignore-auto-dns=true/g'
-
lilydjwg
- 论坛版主
- 帖子: 4258
- 注册时间: 2009-04-11 23:46
- 系统: Arch Linux
-
联系:
#10
帖子
由 lilydjwg » 2012-05-17 23:39
Points 写了:
顺序不能乱哦,加加油。。改下。。

什么程序啊,竟然顺序不能乱。
-
枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
-
联系:
#11
帖子
由 枫叶饭团 » 2012-05-17 23:41
lilydjwg 写了:Points 写了:
顺序不能乱哦,加加油。。改下。。

什么程序啊,竟然顺序不能乱。
我也觉得啊,这货看起来就是个配置文件而已
-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#12
帖子
由 YeLee » 2012-05-17 23:43
-
枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
-
联系:
#13
帖子
由 枫叶饭团 » 2012-05-17 23:44
我本来也打算用正则的,结果在写下re之后又删掉了。蛋疼死了直接==更直接

-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#14
帖子
由 YeLee » 2012-05-17 23:45
貌似是nm的配置文件吧。
-
Points
- 帖子: 83
- 注册时间: 2010-02-19 16:13
#15
帖子
由 Points » 2012-05-17 23:46
YeLee 写了:代码: 全选
sed -e 's/\(type=802-11-wireless\)$/\1\nautoconnect=false/g' -e 's/\(method=auto\)/\1\ndns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;\nignore-auto-dns=true/g'
文件是/home下的123;223;333;444;等多个文件,怎么批量改呀。。
