print the first line of those files in current directory which are having last line with a pattern
I have multiple log files in a directory and want to print first line of those files which are having pattern vanished
on last line.
Could it be done with awk
for *.logs
having also printed name of the logfile before the first line is printed out?
text-processing awk files
add a comment |
I have multiple log files in a directory and want to print first line of those files which are having pattern vanished
on last line.
Could it be done with awk
for *.logs
having also printed name of the logfile before the first line is printed out?
text-processing awk files
add a comment |
I have multiple log files in a directory and want to print first line of those files which are having pattern vanished
on last line.
Could it be done with awk
for *.logs
having also printed name of the logfile before the first line is printed out?
text-processing awk files
I have multiple log files in a directory and want to print first line of those files which are having pattern vanished
on last line.
Could it be done with awk
for *.logs
having also printed name of the logfile before the first line is printed out?
text-processing awk files
text-processing awk files
edited Dec 22 '18 at 17:35
don_crissti
50.3k15134162
50.3k15134162
asked Dec 22 '18 at 14:44
ChrisChris
146112
146112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
something like this?
for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done
You can also do it with awk
but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.
add a comment |
If you want to do this with awk
only this could probably get you in the right direction:
for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done
The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk
And thanks to the comments below, now we can let gawk
do its work as follows:
gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log
On recent linux systems, awk
is a symlink to gawk
. For example, on recent Fedora releases this is the case:
# ls -l /bin/awk
lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk
2
With a reasonably recent GNU awk you probably could avoid the loop e.g.gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log
– steeldriver
Dec 22 '18 at 17:31
2
That wouldn't work with files namesfoo=bar.log
for instance. You can work around it by using./*.log
and replaceFILENAME
withsubstr(FILENAME, 3)
.
– Stéphane Chazelas
Dec 22 '18 at 18:02
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%2f490481%2fprint-the-first-line-of-those-files-in-current-directory-which-are-having-last-l%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
something like this?
for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done
You can also do it with awk
but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.
add a comment |
something like this?
for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done
You can also do it with awk
but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.
add a comment |
something like this?
for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done
You can also do it with awk
but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.
something like this?
for f in *.logs; do tail -n 1 -- "$f" | grep -q vanished && { printf '%s: ' "$f"; head -n 1 -- "$f"; }; done
You can also do it with awk
but it will be less efficient if the files are big, because you will have to read the whole file, not just the beginning and the end.
edited Dec 22 '18 at 18:00
Stéphane Chazelas
301k55565917
301k55565917
answered Dec 22 '18 at 15:04
Uncle BillyUncle Billy
4185
4185
add a comment |
add a comment |
If you want to do this with awk
only this could probably get you in the right direction:
for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done
The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk
And thanks to the comments below, now we can let gawk
do its work as follows:
gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log
On recent linux systems, awk
is a symlink to gawk
. For example, on recent Fedora releases this is the case:
# ls -l /bin/awk
lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk
2
With a reasonably recent GNU awk you probably could avoid the loop e.g.gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log
– steeldriver
Dec 22 '18 at 17:31
2
That wouldn't work with files namesfoo=bar.log
for instance. You can work around it by using./*.log
and replaceFILENAME
withsubstr(FILENAME, 3)
.
– Stéphane Chazelas
Dec 22 '18 at 18:02
add a comment |
If you want to do this with awk
only this could probably get you in the right direction:
for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done
The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk
And thanks to the comments below, now we can let gawk
do its work as follows:
gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log
On recent linux systems, awk
is a symlink to gawk
. For example, on recent Fedora releases this is the case:
# ls -l /bin/awk
lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk
2
With a reasonably recent GNU awk you probably could avoid the loop e.g.gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log
– steeldriver
Dec 22 '18 at 17:31
2
That wouldn't work with files namesfoo=bar.log
for instance. You can work around it by using./*.log
and replaceFILENAME
withsubstr(FILENAME, 3)
.
– Stéphane Chazelas
Dec 22 '18 at 18:02
add a comment |
If you want to do this with awk
only this could probably get you in the right direction:
for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done
The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk
And thanks to the comments below, now we can let gawk
do its work as follows:
gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log
On recent linux systems, awk
is a symlink to gawk
. For example, on recent Fedora releases this is the case:
# ls -l /bin/awk
lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk
If you want to do this with awk
only this could probably get you in the right direction:
for f in *.log; do awk 'NR==1{ first_line=$0 } END { last_line=$0; print( last_line ~ /varnished/) ? "File: " FILENAME "n" first_line : "" }' "$f"; done
The for loop is probably not necessarily but this is the most efficient way I could find doing it with awk
And thanks to the comments below, now we can let gawk
do its work as follows:
gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print substr(FILENAME,3) ":", first}' ./*.log
On recent linux systems, awk
is a symlink to gawk
. For example, on recent Fedora releases this is the case:
# ls -l /bin/awk
lrwxrwxrwx. 1 root root 4 Jul 13 07:55 /bin/awk -> gawk
edited Dec 23 '18 at 13:59
answered Dec 22 '18 at 16:17
Valentin BajramiValentin Bajrami
5,96111627
5,96111627
2
With a reasonably recent GNU awk you probably could avoid the loop e.g.gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log
– steeldriver
Dec 22 '18 at 17:31
2
That wouldn't work with files namesfoo=bar.log
for instance. You can work around it by using./*.log
and replaceFILENAME
withsubstr(FILENAME, 3)
.
– Stéphane Chazelas
Dec 22 '18 at 18:02
add a comment |
2
With a reasonably recent GNU awk you probably could avoid the loop e.g.gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log
– steeldriver
Dec 22 '18 at 17:31
2
That wouldn't work with files namesfoo=bar.log
for instance. You can work around it by using./*.log
and replaceFILENAME
withsubstr(FILENAME, 3)
.
– Stéphane Chazelas
Dec 22 '18 at 18:02
2
2
With a reasonably recent GNU awk you probably could avoid the loop e.g.
gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log
– steeldriver
Dec 22 '18 at 17:31
With a reasonably recent GNU awk you probably could avoid the loop e.g.
gawk 'FNR==1 {first = $0} ENDFILE {if($0 ~ /varnished/) print FILENAME ":", first}' *.log
– steeldriver
Dec 22 '18 at 17:31
2
2
That wouldn't work with files names
foo=bar.log
for instance. You can work around it by using ./*.log
and replace FILENAME
with substr(FILENAME, 3)
.– Stéphane Chazelas
Dec 22 '18 at 18:02
That wouldn't work with files names
foo=bar.log
for instance. You can work around it by using ./*.log
and replace FILENAME
with substr(FILENAME, 3)
.– Stéphane Chazelas
Dec 22 '18 at 18:02
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%2f490481%2fprint-the-first-line-of-those-files-in-current-directory-which-are-having-last-l%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