How to extract filenames from a chunk of code
I want to extract all the filenames which are coming in a piece of code like
a=`cut -d: -f 3 /etc/passwd | sort | uniq` | awk `{print $2}`
"if [ -n "$a" ];",
"then for i in `echo "$a"`; do awk -F:
...
;done; else echo "error";fi" cat /etc/issue ...
/etc/pam.d/system-auth
(/deny=/)
Like in this code, I want to fetch these files -
/etc/passwd, /etc/issue and /etc/pam.d/system-auth
regex linux bash
add a comment |
I want to extract all the filenames which are coming in a piece of code like
a=`cut -d: -f 3 /etc/passwd | sort | uniq` | awk `{print $2}`
"if [ -n "$a" ];",
"then for i in `echo "$a"`; do awk -F:
...
;done; else echo "error";fi" cat /etc/issue ...
/etc/pam.d/system-auth
(/deny=/)
Like in this code, I want to fetch these files -
/etc/passwd, /etc/issue and /etc/pam.d/system-auth
regex linux bash
1
what have you tried so far?
– oguzismail
Nov 23 '18 at 5:38
There are many valid filenames in your sample data:a
,cut
,/etc/passwd
,sort
,uniq
,awk
,print
in the first line alone. You will need to be more specific about what you want to extract.
– Nick
Nov 23 '18 at 5:48
Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
– Cyrus
Nov 23 '18 at 5:48
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 23 '18 at 6:03
add a comment |
I want to extract all the filenames which are coming in a piece of code like
a=`cut -d: -f 3 /etc/passwd | sort | uniq` | awk `{print $2}`
"if [ -n "$a" ];",
"then for i in `echo "$a"`; do awk -F:
...
;done; else echo "error";fi" cat /etc/issue ...
/etc/pam.d/system-auth
(/deny=/)
Like in this code, I want to fetch these files -
/etc/passwd, /etc/issue and /etc/pam.d/system-auth
regex linux bash
I want to extract all the filenames which are coming in a piece of code like
a=`cut -d: -f 3 /etc/passwd | sort | uniq` | awk `{print $2}`
"if [ -n "$a" ];",
"then for i in `echo "$a"`; do awk -F:
...
;done; else echo "error";fi" cat /etc/issue ...
/etc/pam.d/system-auth
(/deny=/)
Like in this code, I want to fetch these files -
/etc/passwd, /etc/issue and /etc/pam.d/system-auth
regex linux bash
regex linux bash
asked Nov 23 '18 at 5:32
Fullmetal
435
435
1
what have you tried so far?
– oguzismail
Nov 23 '18 at 5:38
There are many valid filenames in your sample data:a
,cut
,/etc/passwd
,sort
,uniq
,awk
,print
in the first line alone. You will need to be more specific about what you want to extract.
– Nick
Nov 23 '18 at 5:48
Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
– Cyrus
Nov 23 '18 at 5:48
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 23 '18 at 6:03
add a comment |
1
what have you tried so far?
– oguzismail
Nov 23 '18 at 5:38
There are many valid filenames in your sample data:a
,cut
,/etc/passwd
,sort
,uniq
,awk
,print
in the first line alone. You will need to be more specific about what you want to extract.
– Nick
Nov 23 '18 at 5:48
Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
– Cyrus
Nov 23 '18 at 5:48
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 23 '18 at 6:03
1
1
what have you tried so far?
– oguzismail
Nov 23 '18 at 5:38
what have you tried so far?
– oguzismail
Nov 23 '18 at 5:38
There are many valid filenames in your sample data:
a
, cut
, /etc/passwd
, sort
, uniq
, awk
, print
in the first line alone. You will need to be more specific about what you want to extract.– Nick
Nov 23 '18 at 5:48
There are many valid filenames in your sample data:
a
, cut
, /etc/passwd
, sort
, uniq
, awk
, print
in the first line alone. You will need to be more specific about what you want to extract.– Nick
Nov 23 '18 at 5:48
Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
– Cyrus
Nov 23 '18 at 5:48
Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
– Cyrus
Nov 23 '18 at 5:48
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 23 '18 at 6:03
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 23 '18 at 6:03
add a comment |
1 Answer
1
active
oldest
votes
Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,
/[w+./-]+(?= |$)
Demo
You may put your text in a file say myfile and then run this command,
cat myfile|grep -oP '/[w+./-]+(?= |$)'
OR
grep -oP '/[w+./-]+(?= |$)' myfile
This prints following output as the way you want,
/etc/passwd
/etc/issue
/etc/pam.d/system-auth
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%2f53441104%2fhow-to-extract-filenames-from-a-chunk-of-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,
/[w+./-]+(?= |$)
Demo
You may put your text in a file say myfile and then run this command,
cat myfile|grep -oP '/[w+./-]+(?= |$)'
OR
grep -oP '/[w+./-]+(?= |$)' myfile
This prints following output as the way you want,
/etc/passwd
/etc/issue
/etc/pam.d/system-auth
add a comment |
Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,
/[w+./-]+(?= |$)
Demo
You may put your text in a file say myfile and then run this command,
cat myfile|grep -oP '/[w+./-]+(?= |$)'
OR
grep -oP '/[w+./-]+(?= |$)' myfile
This prints following output as the way you want,
/etc/passwd
/etc/issue
/etc/pam.d/system-auth
add a comment |
Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,
/[w+./-]+(?= |$)
Demo
You may put your text in a file say myfile and then run this command,
cat myfile|grep -oP '/[w+./-]+(?= |$)'
OR
grep -oP '/[w+./-]+(?= |$)' myfile
This prints following output as the way you want,
/etc/passwd
/etc/issue
/etc/pam.d/system-auth
Assuming you are interested in absolute paths that start with a slash, you can use following regex to capture,
/[w+./-]+(?= |$)
Demo
You may put your text in a file say myfile and then run this command,
cat myfile|grep -oP '/[w+./-]+(?= |$)'
OR
grep -oP '/[w+./-]+(?= |$)' myfile
This prints following output as the way you want,
/etc/passwd
/etc/issue
/etc/pam.d/system-auth
edited Nov 23 '18 at 10:17
answered Nov 23 '18 at 9:37
Pushpesh Kumar Rajwanshi
5,4322827
5,4322827
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53441104%2fhow-to-extract-filenames-from-a-chunk-of-code%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
1
what have you tried so far?
– oguzismail
Nov 23 '18 at 5:38
There are many valid filenames in your sample data:
a
,cut
,/etc/passwd
,sort
,uniq
,awk
,print
in the first line alone. You will need to be more specific about what you want to extract.– Nick
Nov 23 '18 at 5:48
Welcome to SO. Stack Overflow is a question and answer page for professional and enthusiastic programmers. Add your own code to your question. You are expected to show at least the amount of research you have put into solving this question yourself.
– Cyrus
Nov 23 '18 at 5:48
Please take a look at: What should I do when someone answers my question?
– Cyrus
Nov 23 '18 at 6:03