Script to remove string up to first number
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 |
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 '18 at 22:18
Updated @choroba
– WebDevB
Nov 21 '18 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 '18 at 22:58
add a comment |
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 '18 at 22:22
WebDevB
asked Nov 21 '18 at 22:16
WebDevBWebDevB
1952219
1952219
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 '18 at 22:18
Updated @choroba
– WebDevB
Nov 21 '18 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 '18 at 22:58
add a comment |
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 '18 at 22:18
Updated @choroba
– WebDevB
Nov 21 '18 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 '18 at 22:58
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 '18 at 22:18
The first number is 15. Please specify the constraints more precisely.
– choroba
Nov 21 '18 at 22:18
Updated @choroba
– WebDevB
Nov 21 '18 at 22:23
Updated @choroba
– WebDevB
Nov 21 '18 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 '18 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 '18 at 22:58
add a comment |
5 Answers
5
active
oldest
votes
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.
add a comment |
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 |
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 |
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 |
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 |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 22:33
ShlomiFShlomiF
855410
855410
add a comment |
add a comment |
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 |
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 |
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 '18 at 22:39
answered Nov 21 '18 at 22:32
sacuLsacuL
30.6k41943
30.6k41943
add a comment |
add a comment |
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 |
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 |
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 '18 at 22:41
answered Nov 21 '18 at 22:34
Johnny BeltranJohnny Beltran
719
719
add a comment |
add a comment |
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 |
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 |
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 '18 at 3:01
stack0114106stack0114106
4,4002422
4,4002422
add a comment |
add a comment |
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 |
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 |
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 '18 at 12:20
answered Nov 21 '18 at 22:19
Lie RyanLie Ryan
45.5k970123
45.5k970123
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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 '18 at 22:18
Updated @choroba
– WebDevB
Nov 21 '18 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 '18 at 22:58