How to have the list of files represented by a regex given in input to a bash script
I'm creating a code for the automatic extraction of bib
records from scientific papers.
In an old version of the script i gave in input the name of the folder where all the pdfs were stored, now I want to give a regex. E.g. before:
./AutoBib.sh Papers/
Now:
./Autobib.sh Papers/*.pdf
In the folder there are, for example 3 pdf files: Shrek.pdf, Fiona.pdf, Donkey.pdf, using my script I should be able to retrieve the doi from all files creating a file where all doi are listed but executing my script it returns the doi of the first file and nothing more.
Here there is my code:
for i in $1; do
doi $i
done
doi is a function that extract the doi from a pdf and puts it in a txt file. When i run the script it returns me only the doi of the first file.
How can I feed a regex in my script and being able to iterate though all files that matches that regex?
linux bash
add a comment |
I'm creating a code for the automatic extraction of bib
records from scientific papers.
In an old version of the script i gave in input the name of the folder where all the pdfs were stored, now I want to give a regex. E.g. before:
./AutoBib.sh Papers/
Now:
./Autobib.sh Papers/*.pdf
In the folder there are, for example 3 pdf files: Shrek.pdf, Fiona.pdf, Donkey.pdf, using my script I should be able to retrieve the doi from all files creating a file where all doi are listed but executing my script it returns the doi of the first file and nothing more.
Here there is my code:
for i in $1; do
doi $i
done
doi is a function that extract the doi from a pdf and puts it in a txt file. When i run the script it returns me only the doi of the first file.
How can I feed a regex in my script and being able to iterate though all files that matches that regex?
linux bash
add a comment |
I'm creating a code for the automatic extraction of bib
records from scientific papers.
In an old version of the script i gave in input the name of the folder where all the pdfs were stored, now I want to give a regex. E.g. before:
./AutoBib.sh Papers/
Now:
./Autobib.sh Papers/*.pdf
In the folder there are, for example 3 pdf files: Shrek.pdf, Fiona.pdf, Donkey.pdf, using my script I should be able to retrieve the doi from all files creating a file where all doi are listed but executing my script it returns the doi of the first file and nothing more.
Here there is my code:
for i in $1; do
doi $i
done
doi is a function that extract the doi from a pdf and puts it in a txt file. When i run the script it returns me only the doi of the first file.
How can I feed a regex in my script and being able to iterate though all files that matches that regex?
linux bash
I'm creating a code for the automatic extraction of bib
records from scientific papers.
In an old version of the script i gave in input the name of the folder where all the pdfs were stored, now I want to give a regex. E.g. before:
./AutoBib.sh Papers/
Now:
./Autobib.sh Papers/*.pdf
In the folder there are, for example 3 pdf files: Shrek.pdf, Fiona.pdf, Donkey.pdf, using my script I should be able to retrieve the doi from all files creating a file where all doi are listed but executing my script it returns the doi of the first file and nothing more.
Here there is my code:
for i in $1; do
doi $i
done
doi is a function that extract the doi from a pdf and puts it in a txt file. When i run the script it returns me only the doi of the first file.
How can I feed a regex in my script and being able to iterate though all files that matches that regex?
linux bash
linux bash
edited Dec 30 '18 at 17:36


cody
8,20631226
8,20631226
asked Dec 30 '18 at 17:24
Giuseppe MinardiGiuseppe Minardi
869
869
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
It's important to understand that Papers/*.pdf
is not a regular expression, it's a wildcard pattern that causes bash
to perform filename expansion, or globbing.
$1
represents the first argument to your script, so your for
loop is only ever iterating over that one argument.
Use $@
to represent all arguments:
for i in "$@"; do
doi "$i"
done
1
or evenfor i; do doi "$i"; done
– Paul Hodges
Dec 31 '18 at 14:58
add a comment |
If you want to filter files within directory by pattern, you can pass this pattern as second script parameter and search for matching files using find
.
Here is the code. It's additionally resistant to filenames containing spaces:
find "$1" -maxdepth 1 -name "$2" -exec doi {} ;
Usage example: ./Autobib.sh Papers/ *.pdf
2
This has multiple problems. You don't want to usels
in scripts and*.pdf
is not a valid regex. Even if it were, you'd need to quote it in order to prevent the shell from expanding it into a list of arguments.
– tripleee
Dec 31 '18 at 7:48
Thanks. I edited answer to usefind
instead ofls
and to make description more relevant.
– Robert Baldyga
Dec 31 '18 at 15:58
1
read
is still not robust, you want something likewhile IFS="" read -r filename
but a much better solution isfind ... -exec doi {} j;
– tripleee
Jan 1 at 12:20
Yeah, looks great to me! Thanks!
– Robert Baldyga
Jan 1 at 12:54
add a comment |
You can just run the ls
command in loop and it will solve your problem.
for x in $(ls $@/*.pdf)
do
echo $x ## if you want only file name you can change this line to echo `basename $x`
done
I have created the same scenario as you mentioned above, refer the snapshot.
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%2f53979816%2fhow-to-have-the-list-of-files-represented-by-a-regex-given-in-input-to-a-bash-sc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's important to understand that Papers/*.pdf
is not a regular expression, it's a wildcard pattern that causes bash
to perform filename expansion, or globbing.
$1
represents the first argument to your script, so your for
loop is only ever iterating over that one argument.
Use $@
to represent all arguments:
for i in "$@"; do
doi "$i"
done
1
or evenfor i; do doi "$i"; done
– Paul Hodges
Dec 31 '18 at 14:58
add a comment |
It's important to understand that Papers/*.pdf
is not a regular expression, it's a wildcard pattern that causes bash
to perform filename expansion, or globbing.
$1
represents the first argument to your script, so your for
loop is only ever iterating over that one argument.
Use $@
to represent all arguments:
for i in "$@"; do
doi "$i"
done
1
or evenfor i; do doi "$i"; done
– Paul Hodges
Dec 31 '18 at 14:58
add a comment |
It's important to understand that Papers/*.pdf
is not a regular expression, it's a wildcard pattern that causes bash
to perform filename expansion, or globbing.
$1
represents the first argument to your script, so your for
loop is only ever iterating over that one argument.
Use $@
to represent all arguments:
for i in "$@"; do
doi "$i"
done
It's important to understand that Papers/*.pdf
is not a regular expression, it's a wildcard pattern that causes bash
to perform filename expansion, or globbing.
$1
represents the first argument to your script, so your for
loop is only ever iterating over that one argument.
Use $@
to represent all arguments:
for i in "$@"; do
doi "$i"
done
edited Dec 30 '18 at 17:50
answered Dec 30 '18 at 17:34


codycody
8,20631226
8,20631226
1
or evenfor i; do doi "$i"; done
– Paul Hodges
Dec 31 '18 at 14:58
add a comment |
1
or evenfor i; do doi "$i"; done
– Paul Hodges
Dec 31 '18 at 14:58
1
1
or even
for i; do doi "$i"; done
– Paul Hodges
Dec 31 '18 at 14:58
or even
for i; do doi "$i"; done
– Paul Hodges
Dec 31 '18 at 14:58
add a comment |
If you want to filter files within directory by pattern, you can pass this pattern as second script parameter and search for matching files using find
.
Here is the code. It's additionally resistant to filenames containing spaces:
find "$1" -maxdepth 1 -name "$2" -exec doi {} ;
Usage example: ./Autobib.sh Papers/ *.pdf
2
This has multiple problems. You don't want to usels
in scripts and*.pdf
is not a valid regex. Even if it were, you'd need to quote it in order to prevent the shell from expanding it into a list of arguments.
– tripleee
Dec 31 '18 at 7:48
Thanks. I edited answer to usefind
instead ofls
and to make description more relevant.
– Robert Baldyga
Dec 31 '18 at 15:58
1
read
is still not robust, you want something likewhile IFS="" read -r filename
but a much better solution isfind ... -exec doi {} j;
– tripleee
Jan 1 at 12:20
Yeah, looks great to me! Thanks!
– Robert Baldyga
Jan 1 at 12:54
add a comment |
If you want to filter files within directory by pattern, you can pass this pattern as second script parameter and search for matching files using find
.
Here is the code. It's additionally resistant to filenames containing spaces:
find "$1" -maxdepth 1 -name "$2" -exec doi {} ;
Usage example: ./Autobib.sh Papers/ *.pdf
2
This has multiple problems. You don't want to usels
in scripts and*.pdf
is not a valid regex. Even if it were, you'd need to quote it in order to prevent the shell from expanding it into a list of arguments.
– tripleee
Dec 31 '18 at 7:48
Thanks. I edited answer to usefind
instead ofls
and to make description more relevant.
– Robert Baldyga
Dec 31 '18 at 15:58
1
read
is still not robust, you want something likewhile IFS="" read -r filename
but a much better solution isfind ... -exec doi {} j;
– tripleee
Jan 1 at 12:20
Yeah, looks great to me! Thanks!
– Robert Baldyga
Jan 1 at 12:54
add a comment |
If you want to filter files within directory by pattern, you can pass this pattern as second script parameter and search for matching files using find
.
Here is the code. It's additionally resistant to filenames containing spaces:
find "$1" -maxdepth 1 -name "$2" -exec doi {} ;
Usage example: ./Autobib.sh Papers/ *.pdf
If you want to filter files within directory by pattern, you can pass this pattern as second script parameter and search for matching files using find
.
Here is the code. It's additionally resistant to filenames containing spaces:
find "$1" -maxdepth 1 -name "$2" -exec doi {} ;
Usage example: ./Autobib.sh Papers/ *.pdf
edited Jan 1 at 12:52
answered Dec 30 '18 at 19:13


Robert BaldygaRobert Baldyga
7715
7715
2
This has multiple problems. You don't want to usels
in scripts and*.pdf
is not a valid regex. Even if it were, you'd need to quote it in order to prevent the shell from expanding it into a list of arguments.
– tripleee
Dec 31 '18 at 7:48
Thanks. I edited answer to usefind
instead ofls
and to make description more relevant.
– Robert Baldyga
Dec 31 '18 at 15:58
1
read
is still not robust, you want something likewhile IFS="" read -r filename
but a much better solution isfind ... -exec doi {} j;
– tripleee
Jan 1 at 12:20
Yeah, looks great to me! Thanks!
– Robert Baldyga
Jan 1 at 12:54
add a comment |
2
This has multiple problems. You don't want to usels
in scripts and*.pdf
is not a valid regex. Even if it were, you'd need to quote it in order to prevent the shell from expanding it into a list of arguments.
– tripleee
Dec 31 '18 at 7:48
Thanks. I edited answer to usefind
instead ofls
and to make description more relevant.
– Robert Baldyga
Dec 31 '18 at 15:58
1
read
is still not robust, you want something likewhile IFS="" read -r filename
but a much better solution isfind ... -exec doi {} j;
– tripleee
Jan 1 at 12:20
Yeah, looks great to me! Thanks!
– Robert Baldyga
Jan 1 at 12:54
2
2
This has multiple problems. You don't want to use
ls
in scripts and *.pdf
is not a valid regex. Even if it were, you'd need to quote it in order to prevent the shell from expanding it into a list of arguments.– tripleee
Dec 31 '18 at 7:48
This has multiple problems. You don't want to use
ls
in scripts and *.pdf
is not a valid regex. Even if it were, you'd need to quote it in order to prevent the shell from expanding it into a list of arguments.– tripleee
Dec 31 '18 at 7:48
Thanks. I edited answer to use
find
instead of ls
and to make description more relevant.– Robert Baldyga
Dec 31 '18 at 15:58
Thanks. I edited answer to use
find
instead of ls
and to make description more relevant.– Robert Baldyga
Dec 31 '18 at 15:58
1
1
read
is still not robust, you want something like while IFS="" read -r filename
but a much better solution is find ... -exec doi {} j;
– tripleee
Jan 1 at 12:20
read
is still not robust, you want something like while IFS="" read -r filename
but a much better solution is find ... -exec doi {} j;
– tripleee
Jan 1 at 12:20
Yeah, looks great to me! Thanks!
– Robert Baldyga
Jan 1 at 12:54
Yeah, looks great to me! Thanks!
– Robert Baldyga
Jan 1 at 12:54
add a comment |
You can just run the ls
command in loop and it will solve your problem.
for x in $(ls $@/*.pdf)
do
echo $x ## if you want only file name you can change this line to echo `basename $x`
done
I have created the same scenario as you mentioned above, refer the snapshot.
add a comment |
You can just run the ls
command in loop and it will solve your problem.
for x in $(ls $@/*.pdf)
do
echo $x ## if you want only file name you can change this line to echo `basename $x`
done
I have created the same scenario as you mentioned above, refer the snapshot.
add a comment |
You can just run the ls
command in loop and it will solve your problem.
for x in $(ls $@/*.pdf)
do
echo $x ## if you want only file name you can change this line to echo `basename $x`
done
I have created the same scenario as you mentioned above, refer the snapshot.
You can just run the ls
command in loop and it will solve your problem.
for x in $(ls $@/*.pdf)
do
echo $x ## if you want only file name you can change this line to echo `basename $x`
done
I have created the same scenario as you mentioned above, refer the snapshot.
answered Jan 2 at 2:56
Mohit RathoreMohit Rathore
1004
1004
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%2f53979816%2fhow-to-have-the-list-of-files-represented-by-a-regex-given-in-input-to-a-bash-sc%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