Find, grep and then create table
I am trying to find all *.md
at ${PWD}
, then grep for lines beginning with title:
and print it along with the absolute filepath -H
and the line number of the pattern match -n
, then I try to output the result in a table with separator -s
as :
find ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1; | column -t -s :;' _ {} ;
This is working till grep but I am unable to create a table.
Also, from the correction suggested by @steeldriver, the exec seems to be happening on each find
result one by one, thereby table is not being created?
Sample Output
/Admin/Specification/Specification.md 2 title Specification
/Admin/GraphicCard/index.md 2 title "GraphicCard"
bash grep find columns exec
|
show 4 more comments
I am trying to find all *.md
at ${PWD}
, then grep for lines beginning with title:
and print it along with the absolute filepath -H
and the line number of the pattern match -n
, then I try to output the result in a table with separator -s
as :
find ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1; | column -t -s :;' _ {} ;
This is working till grep but I am unable to create a table.
Also, from the correction suggested by @steeldriver, the exec seems to be happening on each find
result one by one, thereby table is not being created?
Sample Output
/Admin/Specification/Specification.md 2 title Specification
/Admin/GraphicCard/index.md 2 title "GraphicCard"
bash grep find columns exec
What's the purpose in life of that underscore before{}
? And what actually happens? Sample output would go a long way...
– tink
Jan 10 at 17:03
_
is for$0
– Nikhil
Jan 10 at 17:03
2
Try removing the;
between the grep command and the pipe ...
– steeldriver
Jan 10 at 17:06
@steeldriver Thanks! If I redirect the results asfind ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1 | column -t -s : ' _ {}; > FILE
The columns are not preserved anymore. Could you suggest why?
– Nikhil
Jan 10 at 17:13
1
If yourfind
supports it, you can use{} +
in place of{} ;
– steeldriver
Jan 10 at 17:25
|
show 4 more comments
I am trying to find all *.md
at ${PWD}
, then grep for lines beginning with title:
and print it along with the absolute filepath -H
and the line number of the pattern match -n
, then I try to output the result in a table with separator -s
as :
find ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1; | column -t -s :;' _ {} ;
This is working till grep but I am unable to create a table.
Also, from the correction suggested by @steeldriver, the exec seems to be happening on each find
result one by one, thereby table is not being created?
Sample Output
/Admin/Specification/Specification.md 2 title Specification
/Admin/GraphicCard/index.md 2 title "GraphicCard"
bash grep find columns exec
I am trying to find all *.md
at ${PWD}
, then grep for lines beginning with title:
and print it along with the absolute filepath -H
and the line number of the pattern match -n
, then I try to output the result in a table with separator -s
as :
find ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1; | column -t -s :;' _ {} ;
This is working till grep but I am unable to create a table.
Also, from the correction suggested by @steeldriver, the exec seems to be happening on each find
result one by one, thereby table is not being created?
Sample Output
/Admin/Specification/Specification.md 2 title Specification
/Admin/GraphicCard/index.md 2 title "GraphicCard"
bash grep find columns exec
bash grep find columns exec
edited Jan 10 at 18:27
Rui F Ribeiro
39.5k1479132
39.5k1479132
asked Jan 10 at 16:59
NikhilNikhil
25619
25619
What's the purpose in life of that underscore before{}
? And what actually happens? Sample output would go a long way...
– tink
Jan 10 at 17:03
_
is for$0
– Nikhil
Jan 10 at 17:03
2
Try removing the;
between the grep command and the pipe ...
– steeldriver
Jan 10 at 17:06
@steeldriver Thanks! If I redirect the results asfind ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1 | column -t -s : ' _ {}; > FILE
The columns are not preserved anymore. Could you suggest why?
– Nikhil
Jan 10 at 17:13
1
If yourfind
supports it, you can use{} +
in place of{} ;
– steeldriver
Jan 10 at 17:25
|
show 4 more comments
What's the purpose in life of that underscore before{}
? And what actually happens? Sample output would go a long way...
– tink
Jan 10 at 17:03
_
is for$0
– Nikhil
Jan 10 at 17:03
2
Try removing the;
between the grep command and the pipe ...
– steeldriver
Jan 10 at 17:06
@steeldriver Thanks! If I redirect the results asfind ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1 | column -t -s : ' _ {}; > FILE
The columns are not preserved anymore. Could you suggest why?
– Nikhil
Jan 10 at 17:13
1
If yourfind
supports it, you can use{} +
in place of{} ;
– steeldriver
Jan 10 at 17:25
What's the purpose in life of that underscore before
{}
? And what actually happens? Sample output would go a long way...– tink
Jan 10 at 17:03
What's the purpose in life of that underscore before
{}
? And what actually happens? Sample output would go a long way...– tink
Jan 10 at 17:03
_
is for $0
– Nikhil
Jan 10 at 17:03
_
is for $0
– Nikhil
Jan 10 at 17:03
2
2
Try removing the
;
between the grep command and the pipe ...– steeldriver
Jan 10 at 17:06
Try removing the
;
between the grep command and the pipe ...– steeldriver
Jan 10 at 17:06
@steeldriver Thanks! If I redirect the results as
find ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1 | column -t -s : ' _ {}; > FILE
The columns are not preserved anymore. Could you suggest why?– Nikhil
Jan 10 at 17:13
@steeldriver Thanks! If I redirect the results as
find ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1 | column -t -s : ' _ {}; > FILE
The columns are not preserved anymore. Could you suggest why?– Nikhil
Jan 10 at 17:13
1
1
If your
find
supports it, you can use {} +
in place of {} ;
– steeldriver
Jan 10 at 17:25
If your
find
supports it, you can use {} +
in place of {} ;
– steeldriver
Jan 10 at 17:25
|
show 4 more comments
1 Answer
1
active
oldest
votes
You might be overthinking things. Rather than trying to tabulate the results of grep
, why not let awk
, a prolific table-maker, make the table for you?
find . -type f -name *.md -print0 | xargs -0 awk 'BEGIN { OFS="t" } /^title:/ { print FILENAME, FNR, $0 }'
find
will locate all the md
files you're interested in, and pass them all as arguments to awk
by way of xargs
. We then just have awk
spit out, on lines that match your criterium, the name of the file (which find
will have passed with the full relative path), the record number in the file (i. e. the line number), and the line as seen in the file; all separated by tabs as defined in OFS
, the Output Field Separator.
If you want to be a little more exact with the alignment of your output, you could instead do something like:
/^title:/ { printf( "%35s %4d %sn", FILENAME, FNR, $0 ) }
Also, if you don't want to find | xargs awk
but would rather find -exec
, this also works:
find . -name *.md -type f -exec awk '/expiry/ { printf( "%30s %4d %sn", FILENAME, FNR, $0 ) }' "{}" +
How about-exec
with+;
? Is it better if I am not concerned with portability?
– Nikhil
Jan 10 at 18:23
That will also work just fine, yes. I have added that invocation to the answer.
– DopeGhoti
Jan 10 at 18:26
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
});
}
});
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%2f493762%2ffind-grep-and-then-create-table%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
You might be overthinking things. Rather than trying to tabulate the results of grep
, why not let awk
, a prolific table-maker, make the table for you?
find . -type f -name *.md -print0 | xargs -0 awk 'BEGIN { OFS="t" } /^title:/ { print FILENAME, FNR, $0 }'
find
will locate all the md
files you're interested in, and pass them all as arguments to awk
by way of xargs
. We then just have awk
spit out, on lines that match your criterium, the name of the file (which find
will have passed with the full relative path), the record number in the file (i. e. the line number), and the line as seen in the file; all separated by tabs as defined in OFS
, the Output Field Separator.
If you want to be a little more exact with the alignment of your output, you could instead do something like:
/^title:/ { printf( "%35s %4d %sn", FILENAME, FNR, $0 ) }
Also, if you don't want to find | xargs awk
but would rather find -exec
, this also works:
find . -name *.md -type f -exec awk '/expiry/ { printf( "%30s %4d %sn", FILENAME, FNR, $0 ) }' "{}" +
How about-exec
with+;
? Is it better if I am not concerned with portability?
– Nikhil
Jan 10 at 18:23
That will also work just fine, yes. I have added that invocation to the answer.
– DopeGhoti
Jan 10 at 18:26
add a comment |
You might be overthinking things. Rather than trying to tabulate the results of grep
, why not let awk
, a prolific table-maker, make the table for you?
find . -type f -name *.md -print0 | xargs -0 awk 'BEGIN { OFS="t" } /^title:/ { print FILENAME, FNR, $0 }'
find
will locate all the md
files you're interested in, and pass them all as arguments to awk
by way of xargs
. We then just have awk
spit out, on lines that match your criterium, the name of the file (which find
will have passed with the full relative path), the record number in the file (i. e. the line number), and the line as seen in the file; all separated by tabs as defined in OFS
, the Output Field Separator.
If you want to be a little more exact with the alignment of your output, you could instead do something like:
/^title:/ { printf( "%35s %4d %sn", FILENAME, FNR, $0 ) }
Also, if you don't want to find | xargs awk
but would rather find -exec
, this also works:
find . -name *.md -type f -exec awk '/expiry/ { printf( "%30s %4d %sn", FILENAME, FNR, $0 ) }' "{}" +
How about-exec
with+;
? Is it better if I am not concerned with portability?
– Nikhil
Jan 10 at 18:23
That will also work just fine, yes. I have added that invocation to the answer.
– DopeGhoti
Jan 10 at 18:26
add a comment |
You might be overthinking things. Rather than trying to tabulate the results of grep
, why not let awk
, a prolific table-maker, make the table for you?
find . -type f -name *.md -print0 | xargs -0 awk 'BEGIN { OFS="t" } /^title:/ { print FILENAME, FNR, $0 }'
find
will locate all the md
files you're interested in, and pass them all as arguments to awk
by way of xargs
. We then just have awk
spit out, on lines that match your criterium, the name of the file (which find
will have passed with the full relative path), the record number in the file (i. e. the line number), and the line as seen in the file; all separated by tabs as defined in OFS
, the Output Field Separator.
If you want to be a little more exact with the alignment of your output, you could instead do something like:
/^title:/ { printf( "%35s %4d %sn", FILENAME, FNR, $0 ) }
Also, if you don't want to find | xargs awk
but would rather find -exec
, this also works:
find . -name *.md -type f -exec awk '/expiry/ { printf( "%30s %4d %sn", FILENAME, FNR, $0 ) }' "{}" +
You might be overthinking things. Rather than trying to tabulate the results of grep
, why not let awk
, a prolific table-maker, make the table for you?
find . -type f -name *.md -print0 | xargs -0 awk 'BEGIN { OFS="t" } /^title:/ { print FILENAME, FNR, $0 }'
find
will locate all the md
files you're interested in, and pass them all as arguments to awk
by way of xargs
. We then just have awk
spit out, on lines that match your criterium, the name of the file (which find
will have passed with the full relative path), the record number in the file (i. e. the line number), and the line as seen in the file; all separated by tabs as defined in OFS
, the Output Field Separator.
If you want to be a little more exact with the alignment of your output, you could instead do something like:
/^title:/ { printf( "%35s %4d %sn", FILENAME, FNR, $0 ) }
Also, if you don't want to find | xargs awk
but would rather find -exec
, this also works:
find . -name *.md -type f -exec awk '/expiry/ { printf( "%30s %4d %sn", FILENAME, FNR, $0 ) }' "{}" +
edited Jan 10 at 18:26
answered Jan 10 at 17:32
DopeGhotiDopeGhoti
43.9k55382
43.9k55382
How about-exec
with+;
? Is it better if I am not concerned with portability?
– Nikhil
Jan 10 at 18:23
That will also work just fine, yes. I have added that invocation to the answer.
– DopeGhoti
Jan 10 at 18:26
add a comment |
How about-exec
with+;
? Is it better if I am not concerned with portability?
– Nikhil
Jan 10 at 18:23
That will also work just fine, yes. I have added that invocation to the answer.
– DopeGhoti
Jan 10 at 18:26
How about
-exec
with +;
? Is it better if I am not concerned with portability?– Nikhil
Jan 10 at 18:23
How about
-exec
with +;
? Is it better if I am not concerned with portability?– Nikhil
Jan 10 at 18:23
That will also work just fine, yes. I have added that invocation to the answer.
– DopeGhoti
Jan 10 at 18:26
That will also work just fine, yes. I have added that invocation to the answer.
– DopeGhoti
Jan 10 at 18:26
add a comment |
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%2f493762%2ffind-grep-and-then-create-table%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
What's the purpose in life of that underscore before
{}
? And what actually happens? Sample output would go a long way...– tink
Jan 10 at 17:03
_
is for$0
– Nikhil
Jan 10 at 17:03
2
Try removing the
;
between the grep command and the pipe ...– steeldriver
Jan 10 at 17:06
@steeldriver Thanks! If I redirect the results as
find ${PWD} -type f -name "*.md" -exec bash -c 'i="$1"; grep -HnE "^title:" $1 | column -t -s : ' _ {}; > FILE
The columns are not preserved anymore. Could you suggest why?– Nikhil
Jan 10 at 17:13
1
If your
find
supports it, you can use{} +
in place of{} ;
– steeldriver
Jan 10 at 17:25