Script to remove string up to first number
up vote
-2
down vote
favorite
I need to write a script that will look through a folder and strip out the text from a string of an image.
image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
I need this to be renamed to
8820-AV1.jpg
8820-AV2.jpg
8820-AV3.jpg
8820-AV4.jpg
8820-AV5.jpg
8820-AV6.jpg
Does anyone know of a script like this?
python linux bash
add a comment |
up vote
-2
down vote
favorite
I need to write a script that will look through a folder and strip out the text from a string of an image.
image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
I need this to be renamed to
8820-AV1.jpg
8820-AV2.jpg
8820-AV3.jpg
8820-AV4.jpg
8820-AV5.jpg
8820-AV6.jpg
Does anyone know of a script like this?
python linux bash
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 at 22:18
Updated @choroba
– WebDevB
Nov 21 at 22:23
Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the lastbob-
, remove leading minus signs after removing lowercase characters before the.jpg
, or some other magic? What do you want when there is a space in the filename ?
– Walter A
Nov 21 at 22:58
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I need to write a script that will look through a folder and strip out the text from a string of an image.
image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
I need this to be renamed to
8820-AV1.jpg
8820-AV2.jpg
8820-AV3.jpg
8820-AV4.jpg
8820-AV5.jpg
8820-AV6.jpg
Does anyone know of a script like this?
python linux bash
I need to write a script that will look through a folder and strip out the text from a string of an image.
image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
I need this to be renamed to
8820-AV1.jpg
8820-AV2.jpg
8820-AV3.jpg
8820-AV4.jpg
8820-AV5.jpg
8820-AV6.jpg
Does anyone know of a script like this?
python linux bash
python linux bash
edited Nov 21 at 22:22
asked Nov 21 at 22:16
WebDevB
1952219
1952219
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 at 22:18
Updated @choroba
– WebDevB
Nov 21 at 22:23
Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the lastbob-
, remove leading minus signs after removing lowercase characters before the.jpg
, or some other magic? What do you want when there is a space in the filename ?
– Walter A
Nov 21 at 22:58
add a comment |
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 at 22:18
Updated @choroba
– WebDevB
Nov 21 at 22:23
Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the lastbob-
, remove leading minus signs after removing lowercase characters before the.jpg
, or some other magic? What do you want when there is a space in the filename ?
– Walter A
Nov 21 at 22:58
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 at 22:18
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 at 22:18
Updated @choroba
– WebDevB
Nov 21 at 22:23
Updated @choroba
– WebDevB
Nov 21 at 22:23
Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last
bob-
, remove leading minus signs after removing lowercase characters before the .jpg
, or some other magic? What do you want when there is a space in the filename ?– Walter A
Nov 21 at 22:58
Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last
bob-
, remove leading minus signs after removing lowercase characters before the .jpg
, or some other magic? What do you want when there is a space in the filename ?– Walter A
Nov 21 at 22:58
add a comment |
5 Answers
5
active
oldest
votes
up vote
1
down vote
The most natural way would be to use regular expressions.
Here's an example of a python implementation:
import re
s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
capture_from_first_digit_re = re.compile('D*(d.*)')
print(capture_from_first_digit_re .findall(s)[0])
You can use this as you see fit in your context.
Short explanation:
- D - anything except a digit
- D* - keep going as long as you don't hit a digit
- d - any digit
- .* - anything at all
- (###) - capture the matching ###
Putting it together: capture everything after the first digit you find.
New contributor
add a comment |
up vote
0
down vote
If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:
import os
import re
# replace with the path to your file:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
We can create a test file just for fun to see that it works:
import os
import re
# list out all the filenames to put into our test directory
l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']
# Create Directory
os.mkdir('test')
# add in all the files
for f in l:
open(f'test/{f}','a').close()
# All the files are there
>>> os.listdir('test')
['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']
# rename with the loop provided above:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
# all the filenames have changed
>>> os.listdir('test')
['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']
add a comment |
up vote
0
down vote
The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:
import os
for file in os.listdir('.'):
if '.py' not in file: #not the python script
os.rename(file, file[indexOfFirstNum:])
I haven't tested the script so try it first and modify accordingly before running it in the real folder.
EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.
add a comment |
up vote
0
down vote
Here is the Perl one liner solution:
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
$ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg
$
add a comment |
up vote
0
down vote
You most likely can do this with rename if your distro provides this command. For example:
$ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"
or with PCRE Regex as the expression:
$ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*
rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.
add a comment |
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The most natural way would be to use regular expressions.
Here's an example of a python implementation:
import re
s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
capture_from_first_digit_re = re.compile('D*(d.*)')
print(capture_from_first_digit_re .findall(s)[0])
You can use this as you see fit in your context.
Short explanation:
- D - anything except a digit
- D* - keep going as long as you don't hit a digit
- d - any digit
- .* - anything at all
- (###) - capture the matching ###
Putting it together: capture everything after the first digit you find.
New contributor
add a comment |
up vote
1
down vote
The most natural way would be to use regular expressions.
Here's an example of a python implementation:
import re
s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
capture_from_first_digit_re = re.compile('D*(d.*)')
print(capture_from_first_digit_re .findall(s)[0])
You can use this as you see fit in your context.
Short explanation:
- D - anything except a digit
- D* - keep going as long as you don't hit a digit
- d - any digit
- .* - anything at all
- (###) - capture the matching ###
Putting it together: capture everything after the first digit you find.
New contributor
add a comment |
up vote
1
down vote
up vote
1
down vote
The most natural way would be to use regular expressions.
Here's an example of a python implementation:
import re
s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
capture_from_first_digit_re = re.compile('D*(d.*)')
print(capture_from_first_digit_re .findall(s)[0])
You can use this as you see fit in your context.
Short explanation:
- D - anything except a digit
- D* - keep going as long as you don't hit a digit
- d - any digit
- .* - anything at all
- (###) - capture the matching ###
Putting it together: capture everything after the first digit you find.
New contributor
The most natural way would be to use regular expressions.
Here's an example of a python implementation:
import re
s = 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg'
capture_from_first_digit_re = re.compile('D*(d.*)')
print(capture_from_first_digit_re .findall(s)[0])
You can use this as you see fit in your context.
Short explanation:
- D - anything except a digit
- D* - keep going as long as you don't hit a digit
- d - any digit
- .* - anything at all
- (###) - capture the matching ###
Putting it together: capture everything after the first digit you find.
New contributor
New contributor
answered Nov 21 at 22:33
ShlomiF
1706
1706
New contributor
New contributor
add a comment |
add a comment |
up vote
0
down vote
If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:
import os
import re
# replace with the path to your file:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
We can create a test file just for fun to see that it works:
import os
import re
# list out all the filenames to put into our test directory
l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']
# Create Directory
os.mkdir('test')
# add in all the files
for f in l:
open(f'test/{f}','a').close()
# All the files are there
>>> os.listdir('test')
['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']
# rename with the loop provided above:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
# all the filenames have changed
>>> os.listdir('test')
['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']
add a comment |
up vote
0
down vote
If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:
import os
import re
# replace with the path to your file:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
We can create a test file just for fun to see that it works:
import os
import re
# list out all the filenames to put into our test directory
l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']
# Create Directory
os.mkdir('test')
# add in all the files
for f in l:
open(f'test/{f}','a').close()
# All the files are there
>>> os.listdir('test')
['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']
# rename with the loop provided above:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
# all the filenames have changed
>>> os.listdir('test')
['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']
add a comment |
up vote
0
down vote
up vote
0
down vote
If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:
import os
import re
# replace with the path to your file:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
We can create a test file just for fun to see that it works:
import os
import re
# list out all the filenames to put into our test directory
l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']
# Create Directory
os.mkdir('test')
# add in all the files
for f in l:
open(f'test/{f}','a').close()
# All the files are there
>>> os.listdir('test')
['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']
# rename with the loop provided above:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
# all the filenames have changed
>>> os.listdir('test')
['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']
If you're just trying to strip out all text before the first digit is found in the filename, something along these lines with a relatively simple regex should work in python:
import os
import re
# replace with the path to your file:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
We can create a test file just for fun to see that it works:
import os
import re
# list out all the filenames to put into our test directory
l=['image-w-inch-bob-bob-bob-bob-8820-AV1.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV5.jpg',
'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg']
# Create Directory
os.mkdir('test')
# add in all the files
for f in l:
open(f'test/{f}','a').close()
# All the files are there
>>> os.listdir('test')
['image-w-inch-bob-bob-bob-bob-8820-AV5.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV4.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV6.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV3.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV2.jpg', 'image-w-inch-bob-bob-bob-bob-8820-AV1.jpg']
# rename with the loop provided above:
path = 'test/'
for filename in os.listdir(path):
os.rename(os.path.join(path, filename),
os.path.join(path, re.search('d.*',filename).group()))
# all the filenames have changed
>>> os.listdir('test')
['8820-AV1.jpg', '8820-AV3.jpg', '8820-AV2.jpg', '8820-AV6.jpg', '8820-AV5.jpg', '8820-AV4.jpg']
edited Nov 21 at 22:39
answered Nov 21 at 22:32
sacul
27.3k41638
27.3k41638
add a comment |
add a comment |
up vote
0
down vote
The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:
import os
for file in os.listdir('.'):
if '.py' not in file: #not the python script
os.rename(file, file[indexOfFirstNum:])
I haven't tested the script so try it first and modify accordingly before running it in the real folder.
EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.
add a comment |
up vote
0
down vote
The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:
import os
for file in os.listdir('.'):
if '.py' not in file: #not the python script
os.rename(file, file[indexOfFirstNum:])
I haven't tested the script so try it first and modify accordingly before running it in the real folder.
EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.
add a comment |
up vote
0
down vote
up vote
0
down vote
The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:
import os
for file in os.listdir('.'):
if '.py' not in file: #not the python script
os.rename(file, file[indexOfFirstNum:])
I haven't tested the script so try it first and modify accordingly before running it in the real folder.
EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.
The easiest way to do this would be to locate the python script inside the folder the files are located. Assuming that all the filenames have the same amount of characters before the first number, then you'd need something like:
import os
for file in os.listdir('.'):
if '.py' not in file: #not the python script
os.rename(file, file[indexOfFirstNum:])
I haven't tested the script so try it first and modify accordingly before running it in the real folder.
EDIT: Refer to shlomif's answer if you want the script to be more general. Regular expressions are useful for finding and matching patterns in strings.
edited Nov 21 at 22:41
answered Nov 21 at 22:34
Johnny Beltran
719
719
add a comment |
add a comment |
up vote
0
down vote
Here is the Perl one liner solution:
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
$ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg
$
add a comment |
up vote
0
down vote
Here is the Perl one liner solution:
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
$ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg
$
add a comment |
up vote
0
down vote
up vote
0
down vote
Here is the Perl one liner solution:
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
$ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg
$
Here is the Perl one liner solution:
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 image-w-inch-bob-bob-bob-bob-8820-AV6.jpg
$ perl -ne ' BEGIN { foreach $f (glob("image-w*.jpg")) { $newf=$f, $newf=~s/(.[^d]*)-(d+)(.*)/23/g; rename $f, "$newf" } exit } '
$ ls -l
total 0
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV1.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV2.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV3.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV4.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV5.jpg
-rwxrw-r--+ 1 xxxx xxxx 0 Nov 22 08:23 8820-AV6.jpg
$
answered Nov 22 at 3:01
stack0114106
1,4261416
1,4261416
add a comment |
add a comment |
up vote
0
down vote
You most likely can do this with rename if your distro provides this command. For example:
$ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"
or with PCRE Regex as the expression:
$ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*
rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.
add a comment |
up vote
0
down vote
You most likely can do this with rename if your distro provides this command. For example:
$ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"
or with PCRE Regex as the expression:
$ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*
rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.
add a comment |
up vote
0
down vote
up vote
0
down vote
You most likely can do this with rename if your distro provides this command. For example:
$ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"
or with PCRE Regex as the expression:
$ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*
rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.
You most likely can do this with rename if your distro provides this command. For example:
$ rename 'image-w-inch-bob-bob-bob-bob-' '' image-w-"
or with PCRE Regex as the expression:
$ rename 's/image-w-inch-bob-bob-bob-bob-//' image-w-*
rename can batch rename a set of files according to a pattern match, including by regular expression. Note that the are several versions of rename which accepts different syntax, you should consult your local man and/or the command's help page to determine which version you are using and the precise syntax to use.
edited Nov 22 at 12:20
answered Nov 21 at 22:19
Lie Ryan
43.9k868121
43.9k868121
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421214%2fscript-to-remove-string-up-to-first-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 at 22:18
Updated @choroba
– WebDevB
Nov 21 at 22:23
Your example data doesn't make clear where you want to cut. Should de target have 8.3 characters, start with the first digit, start with the second-last minus sign, removing everyting until the last
bob-
, remove leading minus signs after removing lowercase characters before the.jpg
, or some other magic? What do you want when there is a space in the filename ?– Walter A
Nov 21 at 22:58