4

My platform: Ubuntu linux workstation

In a directory, I havea series 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?

2
  • I cleaned up some of the grammar and clarified the title. Commented Apr 21, 2011 at 15:09
  • Are you replacing abc with def in the file's contents, or in the name? I'll do both. Commented Apr 21, 2011 at 15:10

2 Answers 2

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

should be enough

Sign up to request clarification or add additional context in comments.

Comments

0

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 

1 Comment

Be sure to quote the file name variable, as in "$f".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.