Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to do something like take the part of a wildcard match and use it similarly to a regex match. The command might look something like this

cat foo_* | python magic.py > magic_*

If my file names are

   foo_biz
   foo_baz

I want the output to be in files named

   magic_biz
   magic_baz

effectively changing the file names from foo_* to magic_*.

Is it possible to do this in a shell command? I'm using bash, but an answer for other shells is fine too.

share|improve this question
up vote 5 down vote accepted

If I understand what you're trying to do, you can accomplish it but it takes a bit more work:

for f in foo_*; do python magic.py <"$f" >"${f/foo/magic}"; done

(Note: I also replaced the useless use of cat with a < redirect.)

share|improve this answer

Something just a little different than Gordon's excellent answer:

foos=(foo_*)
suffixes=(${foos[@]#foo_})
for sfx in "${suffixes[@]}"; do
    python magic.py < "foo_$sfx"  > "magic_$sfx"
done
share|improve this answer
$ touch foo_{biz,baz}

$ ls foo_*
foo_baz  foo_biz

$ printf "%s\n" foo_* | sed 's/^foo_/magic_/'
magic_baz
magic_biz
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .