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

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
同目录有多个不同名的文本内容都有相关项目
小白求代码。。 :em03
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

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

#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;这两行吗?
niejieqiang
帖子: 151
注册时间: 2009-05-29 22:05

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

#3

帖子 niejieqiang » 2012-05-17 22:08

perl -ni -e 'print xxx if yy'
上次由 niejieqiang 在 2012-05-17 23:32,总共编辑 1 次。
Points
帖子: 83
注册时间: 2010-02-19 16:13

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

#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

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

#5

帖子 Points » 2012-05-17 23:05

niejieqiang 写了:perl -ni -e 'print xxx if yy'

Sent from my U8800Pro using Tapatalk
兄台能写完整吗,看不懂或。。 :em06
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

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

#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

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

#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]$ 
顺序不能乱哦,加加油。。改下。。 :em09
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

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

#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
来自: 东海硇州,一双管钥。
联系:

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

#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'
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

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

#10

帖子 lilydjwg » 2012-05-17 23:39

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

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

#11

帖子 枫叶饭团 » 2012-05-17 23:41

lilydjwg 写了:
Points 写了: 顺序不能乱哦,加加油。。改下。。 :em09
什么程序啊,竟然顺序不能乱。
我也觉得啊,这货看起来就是个配置文件而已
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

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

#12

帖子 YeLee » 2012-05-17 23:43

这辈子最讨厌正则了。 :em20 :em20 :em20
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

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

#13

帖子 枫叶饭团 » 2012-05-17 23:44

YeLee 写了:这辈子最讨厌正则了。 :em20 :em20 :em20
我本来也打算用正则的,结果在写下re之后又删掉了。蛋疼死了直接==更直接 :em09
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

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

#14

帖子 YeLee » 2012-05-17 23:45

貌似是nm的配置文件吧。
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
Points
帖子: 83
注册时间: 2010-02-19 16:13

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

#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;等多个文件,怎么批量改呀。。 :em04
回复