remove special characters in front of the file names

W

wong_powah

I want to remove all special characters in front of a list of file
names (which may be generated by the "find" or other commands).
How to do that?
This does not work:
$ find . -type f -print | perl -pe 's/([?|*.\'"])/\\$1/g'$ find . -type f -print | perl -pe "s/([?|*.\'"])/\\$1/g"
bash: syntax error near unexpected token `)'
 
J

Jürgen Exner

I want to remove all special characters in front of a list of file
names (which may be generated by the "find" or other commands).
How to do that?
This does not work:

Unfortunately you forgot to tell in which way the expected behaviour is
different from the observed behaviour, i.e. WHAT IS THE PROBLEM?
$ find . -type f -print | perl -pe 's/([?|*.\'"])/\\$1/g'

So I can only guess that this s/// does additional unwanted substitutions in
the middle of the text. This is because you forgot to anchor the RE to the
beginning of the string.

jue
 
J

John W. Krahn

I want to remove all special characters in front of a list of file
names (which may be generated by the "find" or other commands).
How to do that?
This does not work:
$ find . -type f -print | perl -pe 's/([?|*.\'"])/\\$1/g'
$ find . -type f -print | perl -pe "s/([?|*.\'"])/\\$1/g"
bash: syntax error near unexpected token `)'

find . -type f -print | perl -pe 's/([?|*.\047"])/\\$1/g'



John
 
W

wong_powah

I want to remove all special characters in front of a list of file
names (which may be generated by the "find" or other commands).
How to do that?
This does not work:

Unfortunately you forgot to tell in which way the expected behaviour is
different from the observed behaviour, i.e. WHAT IS THE PROBLEM?
$ find . -type f -print | perl -pe 's/([?|*.\'"])/\\$1/g'

So I can only guess that this s/// does additional unwanted substitutions in
the middle of the text. This is because you forgot to anchor the RE to the
beginning of the string.

jue

The problem is that a '>' appear for
$ find . -type f -print | perl -pe 's/([?|*.\'"])/\\$1/g'
Sorry, I don't understand the meaning of "to anchor the RE to the
beginning of the string."

Do you mean:
$ find . -type f -print | perl -pe 's/^([?|*.\'"])/\\$1/g'$ find . -type f -print | perl -pe "s/^([?|*.\'"])/\\$1/g"
bash: syntax error near unexpected token `)'

Both does not work:(.
 
W

wong_powah

e.g. for this output:
$ find . -type f -print
../VERSION
../RELEASE

My desired output will be:
VERSION
RELEASE

This does not work:
$ find . -type f -print | perl -pe 's/([?|*.\047"])/$1/g'
../VERSION
../RELEASE
 
N

nolo contendere

e.g. for this output:
$ find . -type f -print
./VERSION
./RELEASE

My desired output will be:
VERSION
RELEASE

This does not work:
$ find . -type f -print |  perl -pe 's/([?|*.\047"])/$1/g'
./VERSION
./RELEASE


try:

find . -type f -print | perl -pe 's/^\W+//'
 
T

Ted Zlatanov

On Tue, 8 Jan 2008 12:18:08 -0800 (PST) (e-mail address removed) wrote:

wp> e.g. for this output:
wp> $ find . -type f -print
wp> ./VERSION
wp> ./RELEASE

wp> My desired output will be:
wp> VERSION
wp> RELEASE

wp> This does not work:
wp> $ find . -type f -print | perl -pe 's/([?|*.\047"])/$1/g'
wp> ./VERSION
wp> ./RELEASE

You mean you want the base name? This will do what you're asking in the
example above (you don't need Perl necessarily):

find . -type f -exec basename {} \;

Are you trying to do something else? Show some more examples (what you
had above was great, by the way).

Ted
 
J

Jürgen Exner

e.g. for this output:
$ find . -type f -print
./VERSION
./RELEASE

My desired output will be:
VERSION
RELEASE

This does not work:
$ find . -type f -print | perl -pe 's/([?|*.\047"])/$1/g'
./VERSION
./RELEASE

I have a _very_ strong suspicion that we are looking at an x-y-problem.
You want to achieve something and you believe, removing "special" characters
from the beginning of the string is the best way to do it. Therefore you are
asking how to remove special characters. That would be the Y.

From you sample data I gather you are dealing with file names and I'm
guessing you want to get the basename of the file. That would be the X.
The right tool to do that would be File::Basename.

As for blindly removing those characters that you mentioned earlier
[?|*.\'"] are you aware, that in most file system many if not all of them
can be part of a regular file name? Do you really want to change the file
name if it starts with one of your "special" characters?

jue
 
W

wong_powah

On Tue, 8 Jan 2008 12:18:08 -0800 (PST) (e-mail address removed) wrote:

wp> e.g. for this output:
wp> $ find . -type f -print
wp> ./VERSION
wp> ./RELEASE

wp> My desired output will be:
wp> VERSION
wp> RELEASE

wp> This does not work:
wp> $ find . -type f -print | perl -pe 's/([?|*.\047"])/$1/g'
wp> ./VERSION
wp> ./RELEASE

You mean you want the base name? This will do what you're asking in the
example above (you don't need Perl necessarily):

find . -type f -exec basename {} \;

Are you trying to do something else? Show some more examples (what you
had above was great, by the way).

Ted

It turns out that all I want is the dirname and basename.
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top