分页: 1 / 2

请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 21:43
Points
请教高手如何用脚本批量修改同目录多个文件中的指定内容??
原始:

代码: 全选

[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
同目录有多个不同名的文本内容都有相关项目
小白求代码。。 :em03

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 21:50
枫叶饭团
你是说增加autoconnect=false和dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;这两行吗?

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 22:08
niejieqiang
perl -ni -e 'print xxx if yy'

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:03
Points
枫叶饭团 写了:你是说增加autoconnect=false和dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;这两行吗?
还有

代码: 全选

ignore-auto-dns=true
加三行总共是。。

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:05
Points
niejieqiang 写了:perl -ni -e 'print xxx if yy'

Sent from my U8800Pro using Tapatalk
兄台能写完整吗,看不懂或。。 :em06

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 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]$ 

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:32
Points
枫叶饭团 写了:[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]$ 
顺序不能乱哦,加加油。。改下。。 :em09

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 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。。

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:38
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'

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:39
lilydjwg
Points 写了: 顺序不能乱哦,加加油。。改下。。 :em09
什么程序啊,竟然顺序不能乱。

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:41
枫叶饭团
lilydjwg 写了:
Points 写了: 顺序不能乱哦,加加油。。改下。。 :em09
什么程序啊,竟然顺序不能乱。
我也觉得啊,这货看起来就是个配置文件而已

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:43
YeLee
这辈子最讨厌正则了。 :em20 :em20 :em20

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:44
枫叶饭团
YeLee 写了:这辈子最讨厌正则了。 :em20 :em20 :em20
我本来也打算用正则的,结果在写下re之后又删掉了。蛋疼死了直接==更直接 :em09

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:45
YeLee
貌似是nm的配置文件吧。

Re: 请教高手如何用脚本批量修改同目录多个文件中的指定内容??

发表于 : 2012-05-17 23:46
Points
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;等多个文件,怎么批量改呀。。 :em04