1

I have a lot of data with file name (just for example):

dt_upd_global_merged_madt_uv_20100801_20100801_20110721.bil
dt_upd_global_merged_madt_uv_20100802_20100802_20110721.bil
dt_upd_global_merged_madt_uv_20100803_20100803_20110721.bil

What should I do if I want to rename that files into the following?

20100801.bil
20100802.bil
20100803.bil
1

3 Answers 3

2

Try this:

for f in *.bil; do

    n=$(echo "${f}"|sed -r 's/^.+([0-9]{8})_\1_[0-9]{8}[.]bil$/\1/'|grep -Ev '.bil$')
   #or:
   #n=`echo "${f}"|sed -r 's/^.+([0-9]{8})_\1_[0-9]{8}[.]bil$/\1/'|grep -Ev '.bil$'`

    if [ -n "${n}" ]; then
        mv "${f}" "${n}.bil"
    fi
done
0
2

Easiest way with Zsh (by calling zsh first, obviously):

autoload -U zmv
zmv 'dt_upd_global_merged_madt_uv_(*)_(*)_(*).bil' '$1.bil'

Or with the Perl rename:

rename 's/dt_upd_global_merged_madt_uv_(.*)_(.*)_(.*).bil/$1.bil/' *.bil
1

Using awk, -F option to specify _ as the delimiter. The 7th field output plus ".bil" (for example) places 20100801.bil in the fnew variable. Then mv the original name to the new name.

    for f in *.bil
    do
        fnew=$(awk -F_ '{print $7 ".bil"}' <<< $f);
        mv $f $fnew
    done
0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.