Unix Shell - remove trailing characters of filenames

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > remove trailing characters of filenames





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author remove trailing characters of filenames
chris

2006-11-23, 7:33 am

Hi all,
my problem which I want to solve using bash under Linux:
I have a lot of automatically produced files named like
az43_xy-filename
How can I remove with a simple bash script / command these 8 characters
from all the files? I was unable to run rename because it did not
accept regexp.
Thanks for any idea.
Chris
RolandRB

2006-11-23, 7:33 am


chris wrote:
> Hi all,
> my problem which I want to solve using bash under Linux:
> I have a lot of automatically produced files named like
> az43_xy-filename
> How can I remove with a simple bash script / command these 8 characters
> from all the files? I was unable to run rename because it did not
> accept regexp.
> Thanks for any idea.
> Chris


Maybe this script will do it:
http://www.datasavantconsulting.com...scripts/grename

Radoulov, Dimitre

2006-11-23, 7:33 am


"chris" <cjfa04@web.de> wrote in message
news:ek3sq9$1abl$1@gwdu112.gwdg.de...
> Hi all,
> my problem which I want to solve using bash under Linux:
> I have a lot of automatically produced files named like
> az43_xy-filename
> How can I remove with a simple bash script / command these 8 characters
> from all the files?

[...]


$ touch az43_xy-filename1 az44_xy-filename2 az45_xy-filename3
$ set -- *filename*
$ for fname; do
> mv "$fname" "${fname:8}"
> done

$ ls -1 *filename*
filename1
filename2
filename3


With zsh it would be straightforward.


Regards
Dimitre


Stephane CHAZELAS

2006-11-23, 1:16 pm

2006-11-23, 14:27(+01), Radoulov, Dimitre:
>
> "chris" <cjfa04@web.de> wrote in message
> news:ek3sq9$1abl$1@gwdu112.gwdg.de...
> [...]
>
>
> $ touch az43_xy-filename1 az44_xy-filename2 az45_xy-filename3
> $ set -- *filename*
> $ for fname; do
> $ ls -1 *filename*
> filename1
> filename2
> filename3
>
>
> With zsh it would be straightforward.

[...]

Yes, either:

autoload zmv

zmv '????????(*filename*)' '$1'

or

zmv '*filename*' '$f[9,-1]'

for instance.

--
Stéphane
Janis

2006-11-23, 1:16 pm

Radoulov, Dimitre wrote:
> "chris" <cjfa04@web.de> wrote in message
> news:ek3sq9$1abl$1@gwdu112.gwdg.de...

Which 8 characters, the first 8 or the last 8?
[vbcol=seagreen]
> [...]
>
>
> $ touch az43_xy-filename1 az44_xy-filename2 az45_xy-filename3
> $ set -- *filename*
> $ for fname; do

With POSIX shell, and a flexible pattern matching...

mv "$fname" "${fname##*-}"

or if you want to count exactly 8 characters

mv "$fname" "${fname#????????}"

If you want to strip off the last eight characters (or a constant
filename) use either of...

mv "$fname" "${fname%????????}"
mv "$fname" "${fname%filename}"


Janis
[vbcol=seagreen]
> $ ls -1 *filename*
> filename1
> filename2
> filename3
>
>
> With zsh it would be straightforward.
>
>
> Regards
> Dimitre


Radoulov, Dimitre

2006-11-23, 1:16 pm


"Radoulov, Dimitre" <cichomitiko@gmail.com> wrote in message
news:4565a1b3$0$49205$14726298@news.sunsite.dk...
>
> "chris" <cjfa04@web.de> wrote in message
> news:ek3sq9$1abl$1@gwdu112.gwdg.de...
> [...]
>
>
> $ touch az43_xy-filename1 az44_xy-filename2 az45_xy-filename3
> $ set -- *filename*
> $ for fname; do
> $ ls -1 *filename*
> filename1
> filename2
> filename3


May be I misread the question, the OP said the last 8 characters
.... and Janis already provided a solution.



Regards
Dimitre


Chris F.A. Johnson

2006-11-23, 1:16 pm

On 2006-11-23, Radoulov, Dimitre wrote:
>
> "chris" <cjfa04@web.de> wrote in message
> news:ek3sq9$1abl$1@gwdu112.gwdg.de...
> [...]
>
>
> $ touch az43_xy-filename1 az44_xy-filename2 az45_xy-filename3
> $ set -- *filename*
> $ for fname; do


Why not:

for fname in *filename*


That removes the leading 8 characters, not the trailing 8
characters.
[vbcol=seagreen]
> $ ls -1 *filename*
> filename1
> filename2
> filename3



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Chris F.A. Johnson

2006-11-23, 1:16 pm

On 2006-11-23, chris wrote:
> Hi all,
> my problem which I want to solve using bash under Linux:
> I have a lot of automatically produced files named like
> az43_xy-filename
> How can I remove with a simple bash script / command these 8 characters
> from all the files?


for file in * ## adjust pattern if necessary
do
newname=???? ## see below
mv -i "$file" "$newname"
done

Which 8 characters do you want to remove? What do you want the new
filename to be?

If you want to remove the first 8 characters (i.e., rename
az43_xy-filename to filename):

newname=${file#????????}

If you want to remove the last 8 characters (i.e., rename
az43_xy-filename to az43_xy-):

newname=${file%????????}


> I was unable to run rename because it did not
> accept regexp.



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
chris

2006-11-23, 1:16 pm

Hi,
thanks for the help. I explained not exactly enough.
Your suggestion was to remove the first 8 characters:

> for file in * ## adjust pattern if necessary
> do
> newname=???? ## see below
> mv -i "$file" "$newname"
> done
>


>
> newname=${file#????????}


Here is what I tried, given the following filenames:
ab01_cz-sd06112101.abi
ab02_cz-sd06112102.abi
ac01_cz-sd06112103.abi
bd01_cz-sd06112104.abi

for *.abi
do
newname=${*.abi#????????}
mv -i "*.abi" "$newname"
done

It did not want the * , so how can I use regexp in this construct? Or
do I have to use different syntax? Apologies for my minor knowledge
about shell scripting. Thanks for your help.

Chris
Janis

2006-11-23, 1:16 pm

chris wrote:
> Hi,
> thanks for the help. I explained not exactly enough.
> Your suggestion was to remove the first 8 characters:
>
>
>
> Here is what I tried, given the following filenames:
> ab01_cz-sd06112101.abi
> ab02_cz-sd06112102.abi
> ac01_cz-sd06112103.abi
> bd01_cz-sd06112104.abi
>
> for *.abi


for f in *.abi

> do
> newname=${*.abi#????????}
> mv -i "*.abi" "$newname"


mv -i "$f" "$newname"

> done
>
> It did not want the * , so how can I use regexp in this construct? Or
> do I have to use different syntax? Apologies for my minor knowledge
> about shell scripting. Thanks for your help.
>
> Chris


Janis

Janis

2006-11-23, 1:16 pm

chris wrote:
> Hi,
> thanks for the help. I explained not exactly enough.
> Your suggestion was to remove the first 8 characters:
>
>
>
> Here is what I tried, given the following filenames:
> ab01_cz-sd06112101.abi
> ab02_cz-sd06112102.abi
> ac01_cz-sd06112103.abi
> bd01_cz-sd06112104.abi


Please ignore my preceeding posting; I missed another error
in your code.

> for *.abi
> do
> newname=${*.abi#????????}
> mv -i "*.abi" "$newname"
> done


for f in *.abi
do
newname=${f#????????}
mv -i "$f" "$newname"
done

Or do the mv in a single line...

mv -i "$f" "${f#????????}"


Janis

>
> It did not want the * , so how can I use regexp in this construct? Or
> do I have to use different syntax? Apologies for my minor knowledge
> about shell scripting. Thanks for your help.
>
> Chris


Radoulov, Dimitre

2006-11-23, 1:16 pm


"Chris F.A. Johnson" <cfajohnson@gmail.com> wrote in message
news:pklg34-prv.ln1@xword.teksavvy.com...
> On 2006-11-23, Radoulov, Dimitre wrote:
>
> Why not:
>
> for fname in *filename*

[...]


Agreed,
the second statement ("for fname in *filename*") seems more efficient (bash
on Solaris and RHEL).


Regards
Dimitre


Chris F.A. Johnson

2006-11-23, 1:16 pm

On 2006-11-23, chris wrote:
> Hi,
> thanks for the help. I explained not exactly enough.
> Your suggestion was to remove the first 8 characters:


I gave two suggestions; one to remove the first 8, the second to
remove the last 8. Which do you want?

>
>
> Here is what I tried, given the following filenames:
> ab01_cz-sd06112101.abi
> ab02_cz-sd06112102.abi
> ac01_cz-sd06112103.abi
> bd01_cz-sd06112104.abi
>
> for *.abi


for file in *.abi ## place each matching file in turn in $file

> do
> newname=${*.abi#????????}


You must have the name of a variable, not a wildcard, after ${:

newname=${file#????????} ## Remove first 8 characters

> mv -i "*.abi" "$newname"
> done
>
> It did not want the * , so how can I use regexp in this construct? Or
> do I have to use different syntax?


You are not using regexps, you are using file globbing wildcards.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
chris

2006-11-23, 1:16 pm

>
> You are not using regexps, you are using file globbing wildcards.
>

Thanks a lot for your help and explanations. Now I understand it,
now it works like I want it. Good starting point for me to learn
how to use the shell, with LinuxCommand.org I will give it a try.

Chris
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com