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

sh/bash/dash/ksh/zsh等Shell脚本
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

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

#16

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

用*号就行了
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

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

#17

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

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

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

#18

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

能写好不,小白不懂。。
:em06 麻烦呐
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

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

#19

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

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

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

#20

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

YeLee 写了:一次次手工输入多简单啊。 :em04 :em04 :em04
现在就是这么做的,不过想知道捷径以后类似的用的着呀。。 :em09
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

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

#21

帖子 YeLee » 2012-05-18 0:01

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

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

#22

帖子 Points » 2012-05-18 0:26

YeLee 写了:直接在后面加星。 :em04 :em04 :em04
文件111

代码: 全选

[connection]
id=
uuid=
type=802-11-wireless

[ipv4]
method=auto

[ipv6]
method=auto

代码: 全选

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' 111 > 222

代码: 全选

sed -e 's/\(type=802-11-wireless\)$/\1\nautoconnect=false/g' -e 's/\([ipv4]\)/\1\method=auto\ndns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;\nignore-auto-dns=true/g' 111 >222
结果

代码: 全选

[connection]
id=
uuid=
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

[ipv6]
method=auto
[color=#FF0000]dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=true[/color]
红字不要

代码: 全选

[connectimethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=trueon]
imethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=trued=
uuimethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=trued=
typmethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=truee=802-11-wimethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=truereless
autoconnect=false

[imethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=truepmethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=truevmethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=true4method=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=true]
method=auto

[imethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=truepmethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=truevmethod=auto
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;
ignore-auto-dns=true6]
method=auto
Points
帖子: 83
注册时间: 2010-02-19 16:13

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

#23

帖子 Points » 2012-05-18 0:28

代码: 全选

sed -e 's/\(type=802-11-wireless\)$/\1\nautoconnect=false/g' -e 's/\([ipv4]\)/\1\method=auto\ndns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;\nignore-auto-dns=true/g' 111
只出屏显。无文件操作
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

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

#24

帖子 枫叶饭团 » 2012-05-18 0:30

用我的py脚本。直接

代码: 全选

python3 script.py *
就对文件夹下所有文件操作了
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

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

#25

帖子 lilydjwg » 2012-05-18 1:04

Points 写了:

代码: 全选

sed -e 's/\(type=802-11-wireless\)$/\1\nautoconnect=false/g' -e 's/\([ipv4]\)/\1\method=auto\ndns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220;\nignore-auto-dns=true/g' 111
只出屏显。无文件操作

代码: 全选

sed -i ....
头像
link_01
帖子: 1024
注册时间: 2008-11-05 13:24

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

#26

帖子 link_01 » 2012-05-18 10:33

追加文本,脚本中用sed的 "a \"命令方便, :em04
自己在sed后面加 -i 参数,你只给了这么一点文本,错了不要怪我哦 :em09
[bash]#!/bin/sh

files="123 223 333 444"

for file in $files; do
echo "----- $file -----"
sed '1,/\[ipv6\]/{
/type=802-11/a \
authoconnect=false
/method=auto/a \
dns=8.8.8.8;208.67.222.222;8.8.4.4;208.67.220.220; \
ignore-auto-dns=true
}' $file
done
[/bash]
…………………………
怎么问问题都像挤牙膏。
笔记
-------------------------------------
http://blog.163.com/wqt_1101
回复