Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

3 Answers

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
add comment

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
add comment
$ 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.