I'm using this to delete lines that don't match a pattern:$ egrep pattern1|pattern2|pattern3|pattern4|pattern5But now I need to do it automatically, so I'm setting a variable like this:$ catTMP=~/mpp/*.xmlAnd I want to run something like this:$ egrep pattern1|pattern2|pattern3|pattern4|pattern5 $catTMP > $catTMP.2^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^__^Here^How would I base the redirect name off of what variable already exists? If you still don't understand, I mean something like this:$ cd ~/mpp/$ lsx.xml $ egrep pattern1|pattern2|pattern3|pattern4|pattern5 $catTMP > $catTMP.2$ lsx.xml x.xml.2Or even better would be in place editing, but grep can't do that, so maybe there is a way to do this with another command, and just do something like -i.
How can I base the file name off of an already existing variable?
bash;grep;io redirection;variable
Edit in place, keeping only lines that match the pattern:sed -n -r -i /(pattern1|pattern2|pattern3|pattern4|pattern5)/p ~/mpp/*.xmlOn BSD sed (Mac OSX), try:sed -n -E -i '' /(pattern1|pattern2|pattern3|pattern4|pattern5)/p ~/mpp/*.xmlBasing the file name off of an already existing variablefor fname in ~/mpp/*.xmldo    egrep pattern1|pattern2|pattern3|pattern4|pattern5 $fname > $fname.2doneThe above works because a period is not legal in a variable name.  Consequently, the shell knows that, in $fname.2, the variable name is just fname.  If that is not case, then curly braces are required.  For example:for fname in ~/mpp/*.xmldo    egrep pattern1|pattern2|pattern3|pattern4|pattern5 $fname > ${fname}_2doneHere, the curly braces make clear to the shell that the variable name is fname, not fname_2.