Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to replace spaces with different chars?

by kcott (Archbishop)
on Jul 07, 2022 at 08:13 UTC ( [id://11145321]=note: print w/replies, xml ) Need Help??


in reply to How to replace spaces with different chars?

G'day ovedpo15,

"How can I use the system instead?"

With these paths:

ken@titan ~/tmp/pm_11145308_test_dir $ ls -1 a/b/c/d/e 'f:i-:l;e' 'fi:l;e' 'f-i:l;e' 'f-i-:l;e' fi:le

And this code (find_file_match.pl):

#!/usr/bin/env perl use strict; use warnings; my @data = ( 'a/b/c/d/e/fi le', 'a/b/c/d/e/fi l e', 'a/b/c/d/e/f i l e', 'a/b/c/d/e/f i l e', ); for my $datum (@data) { print "\n*** Files matching '$datum':\n"; # $datum =~ y/ /?/; -- see update below $datum =~ s/ /[\\;:,-]/g; system("ls -1 $datum"); }

You get this output:

ken@titan ~/tmp/pm_11145308_test_dir $ ./find_file_match.pl *** Files matching 'a/b/c/d/e/fi le': a/b/c/d/e/fi:le *** Files matching 'a/b/c/d/e/fi l e': 'a/b/c/d/e/fi:l;e' *** Files matching 'a/b/c/d/e/f i l e': 'a/b/c/d/e/f-i:l;e' *** Files matching 'a/b/c/d/e/f i l e': 'a/b/c/d/e/f:i-:l;e' 'a/b/c/d/e/f-i-:l;e'

Update: See ++LanX' valid comment regarding the inherent bug in the code I posted above. Changing y/ /?/ to s/ /[\\;:,-]/g fixes this.

— Ken

Replies are listed 'Best First'.
Re^2: How to replace spaces with different chars?
by LanX (Saint) on Jul 07, 2022 at 10:45 UTC
    I had the same idea, it's far more efficient!

    But you still need a second step where you only grep those files matching [;:,-] at the missing spots, otherwise you will have false positives like fidlee

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      ++ Well spotted.

      If I add fidlee:

      $ ls -1 a/b/c/d/e 'f:i-:l;e' 'fi:l;e' 'f-i:l;e' 'f-i-:l;e' fi:le fidlee

      I get an additional line in the output:

      *** Files matching 'a/b/c/d/e/fi l e': 'a/b/c/d/e/fi:l;e' a/b/c/d/e/fidlee

      Changing y/ /?/ to s/ /[\\;:,-]/g fixes this. I've updated my post.

      — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145321]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found