shell script change filename
shell script change filename
am 04.04.2008 09:20:59 von roman
Hi there
I have a problem regarding changing filenames within a unix shell
script (sh). After testing different approaches for some time (loops
with sed, cut,..) without any success I hope someone of this group has
an answer.
I got hundreds of files like this:
MET8_VIS008_IMG_0703011745.martijn.cal
The part 'MET8_' , 'IMG', 'martijn' is always the same in all files.
Whereas the part 'VIS008' can also be 'IR120' and the date/time
'0703011745' changes (--> '0703011800')
The file has to be renamed like this for further processing:
MET8_VIS008_IMG_200703011745.cal
I really hope someone can help me with an idea.
Thanks a lot in advance. cheers, roman
Re: shell script change filename
am 04.04.2008 09:44:08 von PK
roman wrote:
> Hi there
>
> I have a problem regarding changing filenames within a unix shell
> script (sh). After testing different approaches for some time (loops
> with sed, cut,..) without any success I hope someone of this group has
> an answer.
>
> I got hundreds of files like this:
>
> MET8_VIS008_IMG_0703011745.martijn.cal
>
> The part 'MET8_' , 'IMG', 'martijn' is always the same in all files.
> Whereas the part 'VIS008' can also be 'IR120' and the date/time
> '0703011745' changes (--> '0703011800')
>
> The file has to be renamed like this for further processing:
>
> MET8_VIS008_IMG_200703011745.cal
>
> I really hope someone can help me with an idea.
If your file names don't have strange characters (as it seems), then this
approach might be the simpler:
cd /your/dir
ls | awk -F_ -v OFS="_" -v sq="'" '
{o=$0;$4="20"$4;gsub(/martijn\./,"",$4);
print "mv -- "sq o sq" "sq $0 sq}'
This outputs a series of "mv" commands to rename your files. If those look
fine, then pipe the whole thing to sh to execute it, ie put a "| sh"
(without double quotes) at the end of the above script, after the }'.
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Re: shell script change filename
am 04.04.2008 10:38:34 von Janis Papanagnou
On 4 Apr., 09:20, roman wrote:
> Hi there
>
> I have a problem regarding changing filenames within a unix shell
> script (sh). After testing different approaches for some time (loops
> with sed, cut,..) without any success I hope someone of this group has
> an answer.
>
> I got hundreds of files like this:
>
> MET8_VIS008_IMG_0703011745.martijn.cal
>
> The part 'MET8_' , 'IMG', 'martijn' is always the same in all files.
And what about the .cal?
> Whereas the part 'VIS008' can also be 'IR120' and the date/time
> '0703011745' changes (--> '0703011800')
>
> The file has to be renamed like this for further processing:
>
> MET8_VIS008_IMG_200703011745.cal
So you just want to remove the "martijn"?
for f in MET8_*_IMG_*.martijn.cal
do
mv -i "$f" "${f%.martijn.cal}.cal"
done
Janis
>
> I really hope someone can help me with an idea.
>
> Thanks a lot in advance. cheers, roman
Re: shell script change filename
am 04.04.2008 11:17:49 von PK
roman wrote:
> MET8_VIS008_IMG_0703011745.martijn.cal
>
> The part 'MET8_' , 'IMG', 'martijn' is always the same in all files.
> Whereas the part 'VIS008' can also be 'IR120' and the date/time
> '0703011745' changes (--> '0703011800')
>
> The file has to be renamed like this for further processing:
>
> MET8_VIS008_IMG_200703011745.cal
>
> I really hope someone can help me with an idea.
If you want to use a more shell-oriented approach:
cd /your/dir
for n in *; do
ff=${n%_*}
lf=${n##*_}
lf=20${lf%%.*}.${lf##*.}
# with bash, you could also do
# lf=20${lf/martijn./}
nn="${ff}_${lf}"
# if you're happy with the results,
# comment the next line and uncomment
# the one after it
printf "Old name: %s, New name: %s" "$n" "$nn"
# mv -- "$n" "$nn"
done
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.