Looping over files with specific matching numbers2019 Community Moderator Electioninit.d script is not starting under virtualboxIncrement Numbers in URL?Rename files in bash scriptBash - Regex to match tuples of numbersHow to make bash glob a string variable?Bash - Replacing random number between quotesAddition of extremely large numbers in shell scriptFind filename inside tar archive in different directoryCommand to extract value between two variables and store it in a variableMaintaining several constants for several bash scripts
How does electrical safety system work on ISS?
Creating two special characters
Is this part of the description of the Archfey warlock's Misty Escape feature redundant?
Make a Bowl of Alphabet Soup
awk assign to multiple variables at once
What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?
Is there a nicer/politer/more positive alternative for "negates"?
Why is it that I can sometimes guess the next note?
Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?
Quoting Keynes in a lecture
Strong empirical falsification of quantum mechanics based on vacuum energy density?
Does Doodling or Improvising on the Piano Have Any Benefits?
How to explain what's wrong with this application of the chain rule?
Did the UK lift the requirement for registering SIM cards?
Mimic lecturing on blackboard, facing audience
Why is the "ls" command showing permissions of files in a FAT32 partition?
How to get directions in deep space?
Why is the Sun approximated as a black body at ~ 5800 K?
What's the name of the logical fallacy where a debater extends a statement far beyond the original statement to make it true?
Has any country ever had 2 former presidents in jail simultaneously?
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
What (the heck) is a Super Worm Equinox Moon?
Is there any evidence that Cleopatra and Caesarion considered fleeing to India to escape the Romans?
Which Article Helped Get Rid of Technobabble in RPGs?
Looping over files with specific matching numbers
2019 Community Moderator Electioninit.d script is not starting under virtualboxIncrement Numbers in URL?Rename files in bash scriptBash - Regex to match tuples of numbersHow to make bash glob a string variable?Bash - Replacing random number between quotesAddition of extremely large numbers in shell scriptFind filename inside tar archive in different directoryCommand to extract value between two variables and store it in a variableMaintaining several constants for several bash scripts
I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000.
I have files that are arranged via names that match the starting two digits of each number. For example, one file is called:
/path/to/files11, which is a file corresponding to any number between (and including) 1100 to 1199
Now, given Stop and Start, I want to use a loop that sets the proper files to a string in the following manner: If Start = 1923 and Stop = 2267, then I want to do the following (pseudocode):
MyVariable = "/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22"
but I want to be able to do this for any number in my range.
Any advice on how to select the proper starting and stopping files based on the first two digits and looping over the numbers to get the right string?
bash shell-script
New contributor
add a comment |
I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000.
I have files that are arranged via names that match the starting two digits of each number. For example, one file is called:
/path/to/files11, which is a file corresponding to any number between (and including) 1100 to 1199
Now, given Stop and Start, I want to use a loop that sets the proper files to a string in the following manner: If Start = 1923 and Stop = 2267, then I want to do the following (pseudocode):
MyVariable = "/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22"
but I want to be able to do this for any number in my range.
Any advice on how to select the proper starting and stopping files based on the first two digits and looping over the numbers to get the right string?
bash shell-script
New contributor
add a comment |
I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000.
I have files that are arranged via names that match the starting two digits of each number. For example, one file is called:
/path/to/files11, which is a file corresponding to any number between (and including) 1100 to 1199
Now, given Stop and Start, I want to use a loop that sets the proper files to a string in the following manner: If Start = 1923 and Stop = 2267, then I want to do the following (pseudocode):
MyVariable = "/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22"
but I want to be able to do this for any number in my range.
Any advice on how to select the proper starting and stopping files based on the first two digits and looping over the numbers to get the right string?
bash shell-script
New contributor
I'm using a Bash script, and I have two input numbers. For simplicity, let's say the first variable is called Start and the second variable is Stop, and each can be a number between 1000 and 2000.
I have files that are arranged via names that match the starting two digits of each number. For example, one file is called:
/path/to/files11, which is a file corresponding to any number between (and including) 1100 to 1199
Now, given Stop and Start, I want to use a loop that sets the proper files to a string in the following manner: If Start = 1923 and Stop = 2267, then I want to do the following (pseudocode):
MyVariable = "/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22"
but I want to be able to do this for any number in my range.
Any advice on how to select the proper starting and stopping files based on the first two digits and looping over the numbers to get the right string?
bash shell-script
bash shell-script
New contributor
New contributor
edited yesterday
Rui F Ribeiro
41.6k1483141
41.6k1483141
New contributor
asked yesterday
thetatheta
82
82
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Using bash
Try:
Start=1923
Stop=2267
MyVar=
for ((i=Start/100; i<=Stop/100; i++)); do
MyVar="$MyVar;/path/to/files$i"
done
MyVar=$MyVar:1
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
How it works:
MyVar=
This makes sure that the variable
MyVar
is empty.for ((i=Start/100; i<=Stop/100; i++)); do
This starts a loop over variable
i
.MyVar="$MyVar;/path/to/files$i"
This appends a string to
MyVar
every time the loop is run.done
This signals the end of the loop.
MyVar=$MyVar:1
This removes the unwanted semicolon from the beginning of the string.
echo "$MyVar"
This displays the result.
Using awk
Using very similar logic:
MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
1
Thank you so much!!
– theta
yesterday
add a comment |
This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:
MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")
You then may want to get rid of the final semi-colon via $MyVariable%;
In the above example I'm assuming that your $Start
and $Stop
truly are always at least a 3 digits number, else you will need to adjust the string manipulations.
New contributor
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
theta is a new contributor. Be nice, and check out our Code of Conduct.
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%2funix.stackexchange.com%2fquestions%2f507577%2flooping-over-files-with-specific-matching-numbers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using bash
Try:
Start=1923
Stop=2267
MyVar=
for ((i=Start/100; i<=Stop/100; i++)); do
MyVar="$MyVar;/path/to/files$i"
done
MyVar=$MyVar:1
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
How it works:
MyVar=
This makes sure that the variable
MyVar
is empty.for ((i=Start/100; i<=Stop/100; i++)); do
This starts a loop over variable
i
.MyVar="$MyVar;/path/to/files$i"
This appends a string to
MyVar
every time the loop is run.done
This signals the end of the loop.
MyVar=$MyVar:1
This removes the unwanted semicolon from the beginning of the string.
echo "$MyVar"
This displays the result.
Using awk
Using very similar logic:
MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
1
Thank you so much!!
– theta
yesterday
add a comment |
Using bash
Try:
Start=1923
Stop=2267
MyVar=
for ((i=Start/100; i<=Stop/100; i++)); do
MyVar="$MyVar;/path/to/files$i"
done
MyVar=$MyVar:1
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
How it works:
MyVar=
This makes sure that the variable
MyVar
is empty.for ((i=Start/100; i<=Stop/100; i++)); do
This starts a loop over variable
i
.MyVar="$MyVar;/path/to/files$i"
This appends a string to
MyVar
every time the loop is run.done
This signals the end of the loop.
MyVar=$MyVar:1
This removes the unwanted semicolon from the beginning of the string.
echo "$MyVar"
This displays the result.
Using awk
Using very similar logic:
MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
1
Thank you so much!!
– theta
yesterday
add a comment |
Using bash
Try:
Start=1923
Stop=2267
MyVar=
for ((i=Start/100; i<=Stop/100; i++)); do
MyVar="$MyVar;/path/to/files$i"
done
MyVar=$MyVar:1
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
How it works:
MyVar=
This makes sure that the variable
MyVar
is empty.for ((i=Start/100; i<=Stop/100; i++)); do
This starts a loop over variable
i
.MyVar="$MyVar;/path/to/files$i"
This appends a string to
MyVar
every time the loop is run.done
This signals the end of the loop.
MyVar=$MyVar:1
This removes the unwanted semicolon from the beginning of the string.
echo "$MyVar"
This displays the result.
Using awk
Using very similar logic:
MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
Using bash
Try:
Start=1923
Stop=2267
MyVar=
for ((i=Start/100; i<=Stop/100; i++)); do
MyVar="$MyVar;/path/to/files$i"
done
MyVar=$MyVar:1
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
How it works:
MyVar=
This makes sure that the variable
MyVar
is empty.for ((i=Start/100; i<=Stop/100; i++)); do
This starts a loop over variable
i
.MyVar="$MyVar;/path/to/files$i"
This appends a string to
MyVar
every time the loop is run.done
This signals the end of the loop.
MyVar=$MyVar:1
This removes the unwanted semicolon from the beginning of the string.
echo "$MyVar"
This displays the result.
Using awk
Using very similar logic:
MyVar=$(awk -v Start=1923 -v Stop=2267 'BEGINfor (i=int(Start/100);i<=int(Stop/100);i++) MyVar=MyVar";/path/to/files" i; print substr(MyVar,2)')
echo "$MyVar"
The above echo
statement produces:
/path/to/files19;/path/to/files20;/path/to/files21;/path/to/files22
edited yesterday
answered yesterday
John1024John1024
47.7k5112126
47.7k5112126
1
Thank you so much!!
– theta
yesterday
add a comment |
1
Thank you so much!!
– theta
yesterday
1
1
Thank you so much!!
– theta
yesterday
Thank you so much!!
– theta
yesterday
add a comment |
This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:
MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")
You then may want to get rid of the final semi-colon via $MyVariable%;
In the above example I'm assuming that your $Start
and $Stop
truly are always at least a 3 digits number, else you will need to adjust the string manipulations.
New contributor
add a comment |
This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:
MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")
You then may want to get rid of the final semi-colon via $MyVariable%;
In the above example I'm assuming that your $Start
and $Stop
truly are always at least a 3 digits number, else you will need to adjust the string manipulations.
New contributor
add a comment |
This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:
MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")
You then may want to get rid of the final semi-colon via $MyVariable%;
In the above example I'm assuming that your $Start
and $Stop
truly are always at least a 3 digits number, else you will need to adjust the string manipulations.
New contributor
This looks like a job for Brace Expansion, plus a bit of eval over string manipulation:
MyVariable=$(eval "printf '%s' /path/to/files$Start:0:2..$Stop:0:2;")
You then may want to get rid of the final semi-colon via $MyVariable%;
In the above example I'm assuming that your $Start
and $Stop
truly are always at least a 3 digits number, else you will need to adjust the string manipulations.
New contributor
edited yesterday
New contributor
answered yesterday
LL3LL3
863
863
New contributor
New contributor
add a comment |
add a comment |
theta is a new contributor. Be nice, and check out our Code of Conduct.
theta is a new contributor. Be nice, and check out our Code of Conduct.
theta is a new contributor. Be nice, and check out our Code of Conduct.
theta is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f507577%2flooping-over-files-with-specific-matching-numbers%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