开发者

Rename a file name with regex

开发者 https://www.devze.com 2023-03-13 01:42 出处:网络
I have a set of files (ABC.AM.*.*.20*.*) in a folder e.g.: ABC.AM.00.13.201106.00014, and I need to rename this to ABC.AM.00.13.201106.01014. I am in AIX so rename command is not available.

I have a set of files (ABC.AM.*.*.20*.*) in a folder

e.g.: ABC.AM.00.13.201106.00014, and I need to rename this to ABC.AM.00.13.201106.01014. I am in AIX so rename command is not available.

just a pseudo script

for i in `ls ABC.AM.*.*.20*.*`
do
        mv $i DESIRED_file_name
done

Any help will be very app开发者_如何转开发reciated


Try this:

for arg in ABC.AM.*.*.20*.*
do
  newarg="$(echo "$arg" | sed 's/\(.*\..\)0\(.*\)/\11\2/')"
  if [ "$newarg" != "" ];then
    mv "$arg" "$newarg" 
  fi
done


It's not clear exactly what the replacement is that you want to make, but for the particular example you give, you can do:

for ...; do
    mv $i ${i/00014/01014}
done

A common technique for this sort of thing is to use sed to generate a new filename:

for old_name in ...; do
  new_name=$( echo $old_name | sed -e '...' )
  mv $old_name $new_name
done

When doing this sort of thing, it is a good idea to replace 'mv' with 'echo' and make sure you have the replacement that you want, then re-run the command using 'mv'.

In your case, you might be wanting to use something like:

sed -e 's/000\([0-9]\{2\}\)$/010\1/'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号