Need help with sed or awk and regular expressions

inode0 inode0 at gmail.com
Sat Jul 2 16:53:58 UTC 2005


On 7/2/05, Michael Ault <mikerault at yahoo.com> wrote:
> Excellent, so to pass in a list of file names to be
> converted you would pipe the results of the ls command
> through this? We have a list of about 100 files that
> need to be renamed.

This makes me a little nervous, are we really really sure that all the
shorter names will be unique after we chop things up? Here is how I
would proceed from a bash shell directly ...

Make sure extglob is on:

$ shopt extglob
extglob         on

If it is off run

$ shopt -s extglob

Here are some sample files in the current directory ...

$ ls
44.MR3M01J.AF_TBL_COL_1.2005-06-30.DAT  44.MR3M01J.CF_TBL_COL_2.2005-06-30.DAT
44.MR3M01J.BF_TBL_COL_2.2005-06-30.DAT  44.MR3M01J.DF_TBL_COL_1.2005-06-30.DAT

I like to echo dangerous things first and carefully check that
everything looks right before actually running the command (good idea
to back up the directory first to be safe too).

$ for f in *; do echo mv $f ${f//+(44.MR3M01J.|_[12]*[0-9])}; done
mv 44.MR3M01J.AF_TBL_COL_1.2005-06-30.DAT AF_TBL_COL.DAT
mv 44.MR3M01J.BF_TBL_COL_2.2005-06-30.DAT BF_TBL_COL.DAT
mv 44.MR3M01J.CF_TBL_COL_2.2005-06-30.DAT CF_TBL_COL.DAT
mv 44.MR3M01J.DF_TBL_COL_1.2005-06-30.DAT DF_TBL_COL.DAT

If you are satisfied that all the renames are correct, please
especially check that each target is unique since if you have both
44.MR3M01J.AF_TBL_COL_1.2005-06-30.DAT and
44.MR3M01J.AF_TBL_COL_2.2005-06-30.DAT for instance there will be a
problem, you can remove the echo from the last command to run it

$ for f in *; do mv $f ${f//+(44.MR3M01J.|_[12]*[0-9])}; done
$ ls
AF_TBL_COL.DAT  BF_TBL_COL.DAT  CF_TBL_COL.DAT  DF_TBL_COL.DAT

Hope that helps. Be careful. :)

John




More information about the Redhat-install-list mailing list