Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > perl.beginners > #19376 > unrolled thread

Removing a space from file name

Started byhput3@fastmail.fm (hput)
First post2025-02-17 12:38 -0500
Last post2025-02-19 18:03 -0700
Articles 5 — 4 participants

Back to article view | Back to perl.beginners


Contents

  Removing a space from file name hput3@fastmail.fm (hput) - 2025-02-17 12:38 -0500
    Re: Removing a space from file name paul@pjcj.net - 2025-02-17 19:36 +0100
    Re: Removing a space from file name beginners@perl.org (Mike Hübschen via beginners) - 2025-02-17 19:52 +0100
    Re: Removing a space from file name beginners@perl.org (Smoot Carl-Mitchell via beginners) - 2025-02-17 13:12 -0700
      Re: Removing a space from file name beginners@perl.org (Smoot Carl-Mitchell via beginners) - 2025-02-19 18:03 -0700

#19376 — Removing a space from file name

Fromhput3@fastmail.fm (hput)
Date2025-02-17 12:38 -0500
SubjectRemoving a space from file name
Message-ID<878qq4o4j3.fsf@local.lan>
I have several hundred *.jpg files with this pattern

Actual example:

   'AtlantaVisitAndGrandmaHands 056.jpg'

The numbers vary, of course, but the alpha part and number of digits
after the space is the same in all *.jpg files

I have hundreds of these all in one directory.  Not something you'd
want to do one at a time.

I tried to figure a way using the perl `rename' tool found in many linux
distro's repos

But couldnt figure out the necessary syntax from Larry Wall's examples
in the rename man page.  Using the "s///" operators ,  it's probably
there some how but my pea brain isn't seeimg it.

bash has just used the apostrophes to make the names usable but
I would much rather remove the space.

[toc] | [next] | [standalone]


#19377

Frompaul@pjcj.net
Date2025-02-17 19:36 +0100
Message-ID<d21b379eff634aa8b09353c79bea6960@pjcj.net>
In reply to#19376
On 2025-02-17 18:38, hput wrote:
> I have several hundred *.jpg files with this pattern
> 
> Actual example:
> 
>    'AtlantaVisitAndGrandmaHands 056.jpg'

> I tried to figure a way using the perl `rename' tool found in many 
> linux
> distro's repos
> 
> But couldnt figure out the necessary syntax from Larry Wall's examples
> in the rename man page.  Using the "s///" operators ,  it's probably
> there some how but my pea brain isn't seeimg it.

Almost there.  Try: rename 's/ //' *.jpg

-- 
Paul Johnson - paul@pjcj.net

[toc] | [prev] | [next] | [standalone]


#19378

Frombeginners@perl.org (Mike Hübschen via beginners)
Date2025-02-17 19:52 +0100
Message-ID<f5771210-7051-4fb2-b7a5-99f513692299@web.de>
In reply to#19376
Hello,

Try something like this: rename 's/(.*?)\s(\d+).jpg$/$1$2.jpg/' *.jpg
But make a backup first.

Best regards,

Mike


On 2/17/25 18:38, hput wrote:
> I have several hundred *.jpg files with this pattern
>
> Actual example:
>
>     'AtlantaVisitAndGrandmaHands 056.jpg'
>
> The numbers vary, of course, but the alpha part and number of digits
> after the space is the same in all *.jpg files
>
> I have hundreds of these all in one directory.  Not something you'd
> want to do one at a time.
>
> I tried to figure a way using the perl `rename' tool found in many linux
> distro's repos
>
> But couldnt figure out the necessary syntax from Larry Wall's examples
> in the rename man page.  Using the "s///" operators ,  it's probably
> there some how but my pea brain isn't seeimg it.
>
> bash has just used the apostrophes to make the names usable but
> I would much rather remove the space.
>
>

[toc] | [prev] | [next] | [standalone]


#19379

Frombeginners@perl.org (Smoot Carl-Mitchell via beginners)
Date2025-02-17 13:12 -0700
Message-ID<81759c5d490fe6f278dc46cdb1c4730c9cb2fa47.camel@tic.com>
In reply to#19376
On Mon, 2025-02-17 at 12:38 -0500, hput wrote:
> I have several hundred *.jpg files with this pattern
> 
> Actual example:
> 
>    'AtlantaVisitAndGrandmaHands 056.jpg'
> 
> The numbers vary, of course, but the alpha part and number of digits
> after the space is the same in all *.jpg files
> 
> I have hundreds of these all in one directory.  Not something you'd
> want to do one at a time.

Something like:

while (<*>) {
	rename $_, "$1$2.jpg" if $_ =~ /(.+) (.+)\.jpg/;
}


The RE matches any filename with a single space character and the .jpg
ending. I made the $_ explicit for clarity.
-- 
Smoot Carl-Mitchell
System/Network Architect
voice: +1 480 922-7313
cell: +1 602 421-9005
smoot@tic.com

[toc] | [prev] | [next] | [standalone]


#19380

Frombeginners@perl.org (Smoot Carl-Mitchell via beginners)
Date2025-02-19 18:03 -0700
Message-ID<f17f25f210f780230e3dfe92607b4ba708a8074b.camel@tic.com>
In reply to#19379
On Thu, 2025-02-20 at 08:01 +0800, walt via beginners wrote:
> > 
> > Something like:
> > 
> > while (<*>) {
> > 	rename $_, "$1$2.jpg" if $_ =~ /(.+) (.+)\.jpg/;
> > }
> > 
> 
> what does <*> mean here?

<*> iterates over the current directory and returns every filename in
the directory to the $_ variable. The '*" can be any glob pattern. e.g.
for the specific jpg files in the example, I could have used <*.jpg>
-- 
Smoot Carl-Mitchell
System/Network Architect
voice: +1 480 922-7313
cell: +1 602 421-9005
smoot@tic.com

[toc] | [prev] | [standalone]


Back to top | Article view | perl.beginners


csiph-web