Operating System - Linux
1745809 Members
3743 Online
108722 Solutions
New Discussion юеВ

Re: simplest way to strip a character from a filename

 
SOLVED
Go to solution
Daavid Turnbull
Frequent Advisor

simplest way to strip a character from a filename

I have a gazzilion tiny files each with multiple ':' in the file name. I need to strip these out.

A sample file name is: x_0041257026_102110535974_2006-01-26T20:33:31.272Z.xml.28_00_05.Z

I wrote a clumbsy perl script to do it which is working but it soooo slooooow.

Is there a mean, easy and efficient way to do this?
Behold the turtle for he makes not progress unless he pokes his head out.
16 REPLIES 16
James R. Ferguson
Acclaimed Contributor

Re: simplest way to strip a character from a filename

Hi David:

# perl -ple 's/\.//g;s/Z$/\.Z/'

As for example:

# echo "12.3\nabc_def.xyz\ndavid.Z"|perl -ple 's/\.//g;s/Z$/\.Z/'

(or):

# perl -ple 's/\.//g;s/Z$/\.Z/' fileofnames

This (crudely) preserves the ".Z" extension if present.

Regards!

...JRF...


Geoff Wild
Honored Contributor

Re: simplest way to strip a character from a filename

Don't have a solution for the renaming, but as far as generation of the files - did you fix that so that it doesn't do the :

Looks like a time stamp...

Could do something like:

date '+%FT%H%M%S'

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Sandman!
Honored Contributor

Re: simplest way to strip a character from a filename

Hi,

Try this awk construct...

# ls -1 | awk '{gsub(":",""); print $0}'

cheers!
Daavid Turnbull
Frequent Advisor

Re: simplest way to strip a character from a filename

I am not sure that this is particularly efficient because it invokes perl for each mv but this is currently doing what I want:

for file in *
> do
> mv $file `echo $file | perl -ple 's/\://g'`
> done

Note that the char I wished to replace was a : and not a .

Behold the turtle for he makes not progress unless he pokes his head out.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: simplest way to strip a character from a filename

Something like this should work leveraging the tr command:

ls | while read FNAME
do
FNAME2=$(echo "${FNAME}" | tr -d ":")
if [[ "${FNAME}" != "${FNAME2}" && -n "${FNAME2}" ]]
then
if [[ -r "${FNAME2}" ]]
then
echo "${FNAME2} exists; can't mv" >&2
else
mv "${FNAME}" "${FNAME2}"
fi
fi
done



The idea is that we use tr -d to strip the ':'s from the filename. Next we check to see if the filenames are then diffirent and also that the filename still has a non-zero length. Next we make sure that the new filename does not already exist; if so, that file is skipped. Finally, after all the tests have passed we mv the old name to the new.

Note the "'s around each filename. Whitespace is perfectly legal (if dumb) in UNIX pathnames.

This should be a robust solution.
If it ain't broke, I can fix that.
Muthukumar_5
Honored Contributor

Re: simplest way to strip a character from a filename

for file in *
> do
> mv $file `echo $file | perl -ple 's/\://g'`
> done
==

Simply as,

for file in `ls`
do

mv ${file} $(echo ${file} | perl -pe 's/://g')

done

--
Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: simplest way to strip a character from a filename

There is no need to negate with \ for : character. IT is needed for * ? \ / characters. Simply use s/://g is enough for pattern change.

--
Muthu
Easy to suggest when don't know about the problem!
Daavid Turnbull
Frequent Advisor

Re: simplest way to strip a character from a filename

Because it needs to be done a gazzillion times (well somewhere between 100000 and 1000000) am I correct in assuming that the over head of starting perl as opposed to tr would be a factor in the load it places on the machine?
Behold the turtle for he makes not progress unless he pokes his head out.
Arunvijai_4
Honored Contributor

Re: simplest way to strip a character from a filename

Hi David,

For processing large number of files, perl is the best way to go. It has proven ability when it comes to large number.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"