开发者

How to search-replace content within files in Ubuntu Linux?

My platform: Ubuntu linux workstation

In a directory, I havea serie开发者_开发技巧s of files, with file name xxx_1.in to xxx_50.in

For each file, I want to replace abc to def. If I do it individually, I should type :g/abc/s//def/g

How to write a script to process all of the files at once?


sed -i 's/abc/def/g' xxx_*.in

should be enough


I like Python, even for shell scripting. I can't really figure out how to use sed, so I stick to Python's search and replace:

#!/usr/bin/env python
import os, glob

for filename in glob('xxx_*.in'):
  os.rename(filename , filename .replace('abc', 'def'))

So as an inline script which runs when you copy/paste it into Terminal (I don't have Python on my current machine, so no guarantees),

python -c "import os, glob; eval('for f in glob(\'xxx_*.in\'):\n  os.rename(filename , filename .replace(\'abc\', \'def\'))'"

It seems like you meant the file's contents. That, I can do without Python (hopefully this works):

for f in xxx_*.in; do sed s/abc/def/g "$f"; done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜