How to grep with multiple filters and print one by one?2019 Community Moderator Electiongrep multiple strings, count lines, echo output for each stringGrep multiple pattern negative matchHow to do a grep on remote machine and print out the line which contains those words?grep and print all pattern in one lanegrep. Find multiple AND patterns in any order using single conditiongrep with pattern from one file (3.2Gb) matching in another file (4.8Gb)grep dynamically multiple files with customized outputgrep: detect multi-line pattern with double capturegrep doesn't print matchesTrying to filter out maillog file with multiple “AND” matches
creating a ":KeepCursor" command
User Story breakdown - Technical Task + User Feature
What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?
Invalid date error by date command
Why does a simple loop result in ASYNC_NETWORK_IO waits?
How to explain what's wrong with this application of the chain rule?
Why is this estimator biased?
Recommended PCB layout understanding - ADM2572 datasheet
How to cover method return statement in Apex Class?
Biological Blimps: Propulsion
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
Temporarily disable WLAN internet access for children, but allow it for adults
How to say when an application is taking the half of your screen on a computer
Mixing PEX brands
What should you do if you miss a job interview (deliberately)?
Calculating total slots
Multiplicative persistence
Did arcade monitors have same pixel aspect ratio as TV sets?
Is there a way to get `mathscr' with lower case letters in pdfLaTeX?
What exact color does ozone gas have?
Does the UK parliament need to pass secondary legislation to accept the Article 50 extension
How could a planet have erratic days?
Probability that THHT occurs in a sequence of 10 coin tosses
Mimic lecturing on blackboard, facing audience
How to grep with multiple filters and print one by one?
2019 Community Moderator Electiongrep multiple strings, count lines, echo output for each stringGrep multiple pattern negative matchHow to do a grep on remote machine and print out the line which contains those words?grep and print all pattern in one lanegrep. Find multiple AND patterns in any order using single conditiongrep with pattern from one file (3.2Gb) matching in another file (4.8Gb)grep dynamically multiple files with customized outputgrep: detect multi-line pattern with double capturegrep doesn't print matchesTrying to filter out maillog file with multiple “AND” matches
I need count some patterns in logs, I can use
grep aaa ./logs | wc -l
grep bbb ./logs | wc -l
is there a easy way to do all things in one line? like
cat ./logs | grep -c aaa | grep -c bbb #didn't work.
linux grep
migrated from serverfault.com yesterday
This question came from our site for system and network administrators.
add a comment |
I need count some patterns in logs, I can use
grep aaa ./logs | wc -l
grep bbb ./logs | wc -l
is there a easy way to do all things in one line? like
cat ./logs | grep -c aaa | grep -c bbb #didn't work.
linux grep
migrated from serverfault.com yesterday
This question came from our site for system and network administrators.
add a comment |
I need count some patterns in logs, I can use
grep aaa ./logs | wc -l
grep bbb ./logs | wc -l
is there a easy way to do all things in one line? like
cat ./logs | grep -c aaa | grep -c bbb #didn't work.
linux grep
I need count some patterns in logs, I can use
grep aaa ./logs | wc -l
grep bbb ./logs | wc -l
is there a easy way to do all things in one line? like
cat ./logs | grep -c aaa | grep -c bbb #didn't work.
linux grep
linux grep
edited yesterday
Prvt_Yadv
2,88031227
2,88031227
asked Mar 13 at 12:18
J Wang
migrated from serverfault.com yesterday
This question came from our site for system and network administrators.
migrated from serverfault.com yesterday
This question came from our site for system and network administrators.
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You can use the following command:
$ grep -oh -e aaa -e bbb ./logs | sort | uniq -c
From man grep, you can read:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Also, for -h:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to
search.
The -e is used to match either one. Then, the results are sorted and counted using uniq.
Thanks a lot~~~
– J Wang
Mar 14 at 6:50
Strictly speaking, this will count the various strings that the patterns matches, not how many times each pattern matches. This makes a difference if the patterns are something other than plain strings.
– Kusalananda
yesterday
add a comment |
You can use the -e PATTERN flag.
In order to count how many lines contain either or both aaa or bbb:
grep -e aaa -e bbb ./logs | wc -l
If you want to count aaa and bbb separately, check the solution by Khaled.
add a comment |
Using awk:
awk '/aaa/ count["aaa"]++
/bbb/ count["bbb"]++
END for (pat in count) print count[pat], pat ' file
This would update the count associated with the matching pattern whenever that pattern matches. At the end, a list of counts and the corresponding pattern are outputted.
add a comment |
I Tried with below command
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
output
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
aaa 1
bbb 2
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%2f507734%2fhow-to-grep-with-multiple-filters-and-print-one-by-one%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the following command:
$ grep -oh -e aaa -e bbb ./logs | sort | uniq -c
From man grep, you can read:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Also, for -h:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to
search.
The -e is used to match either one. Then, the results are sorted and counted using uniq.
Thanks a lot~~~
– J Wang
Mar 14 at 6:50
Strictly speaking, this will count the various strings that the patterns matches, not how many times each pattern matches. This makes a difference if the patterns are something other than plain strings.
– Kusalananda
yesterday
add a comment |
You can use the following command:
$ grep -oh -e aaa -e bbb ./logs | sort | uniq -c
From man grep, you can read:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Also, for -h:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to
search.
The -e is used to match either one. Then, the results are sorted and counted using uniq.
Thanks a lot~~~
– J Wang
Mar 14 at 6:50
Strictly speaking, this will count the various strings that the patterns matches, not how many times each pattern matches. This makes a difference if the patterns are something other than plain strings.
– Kusalananda
yesterday
add a comment |
You can use the following command:
$ grep -oh -e aaa -e bbb ./logs | sort | uniq -c
From man grep, you can read:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Also, for -h:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to
search.
The -e is used to match either one. Then, the results are sorted and counted using uniq.
You can use the following command:
$ grep -oh -e aaa -e bbb ./logs | sort | uniq -c
From man grep, you can read:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
Also, for -h:
-h, --no-filename
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to
search.
The -e is used to match either one. Then, the results are sorted and counted using uniq.
answered Mar 13 at 12:25
KhaledKhaled
28625
28625
Thanks a lot~~~
– J Wang
Mar 14 at 6:50
Strictly speaking, this will count the various strings that the patterns matches, not how many times each pattern matches. This makes a difference if the patterns are something other than plain strings.
– Kusalananda
yesterday
add a comment |
Thanks a lot~~~
– J Wang
Mar 14 at 6:50
Strictly speaking, this will count the various strings that the patterns matches, not how many times each pattern matches. This makes a difference if the patterns are something other than plain strings.
– Kusalananda
yesterday
Thanks a lot~~~
– J Wang
Mar 14 at 6:50
Thanks a lot~~~
– J Wang
Mar 14 at 6:50
Strictly speaking, this will count the various strings that the patterns matches, not how many times each pattern matches. This makes a difference if the patterns are something other than plain strings.
– Kusalananda
yesterday
Strictly speaking, this will count the various strings that the patterns matches, not how many times each pattern matches. This makes a difference if the patterns are something other than plain strings.
– Kusalananda
yesterday
add a comment |
You can use the -e PATTERN flag.
In order to count how many lines contain either or both aaa or bbb:
grep -e aaa -e bbb ./logs | wc -l
If you want to count aaa and bbb separately, check the solution by Khaled.
add a comment |
You can use the -e PATTERN flag.
In order to count how many lines contain either or both aaa or bbb:
grep -e aaa -e bbb ./logs | wc -l
If you want to count aaa and bbb separately, check the solution by Khaled.
add a comment |
You can use the -e PATTERN flag.
In order to count how many lines contain either or both aaa or bbb:
grep -e aaa -e bbb ./logs | wc -l
If you want to count aaa and bbb separately, check the solution by Khaled.
You can use the -e PATTERN flag.
In order to count how many lines contain either or both aaa or bbb:
grep -e aaa -e bbb ./logs | wc -l
If you want to count aaa and bbb separately, check the solution by Khaled.
answered Mar 13 at 12:25
jornanejornane
203212
203212
add a comment |
add a comment |
Using awk:
awk '/aaa/ count["aaa"]++
/bbb/ count["bbb"]++
END for (pat in count) print count[pat], pat ' file
This would update the count associated with the matching pattern whenever that pattern matches. At the end, a list of counts and the corresponding pattern are outputted.
add a comment |
Using awk:
awk '/aaa/ count["aaa"]++
/bbb/ count["bbb"]++
END for (pat in count) print count[pat], pat ' file
This would update the count associated with the matching pattern whenever that pattern matches. At the end, a list of counts and the corresponding pattern are outputted.
add a comment |
Using awk:
awk '/aaa/ count["aaa"]++
/bbb/ count["bbb"]++
END for (pat in count) print count[pat], pat ' file
This would update the count associated with the matching pattern whenever that pattern matches. At the end, a list of counts and the corresponding pattern are outputted.
Using awk:
awk '/aaa/ count["aaa"]++
/bbb/ count["bbb"]++
END for (pat in count) print count[pat], pat ' file
This would update the count associated with the matching pattern whenever that pattern matches. At the end, a list of counts and the corresponding pattern are outputted.
edited yesterday
answered yesterday
KusalanandaKusalananda
137k17258426
137k17258426
add a comment |
add a comment |
I Tried with below command
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
output
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
aaa 1
bbb 2
add a comment |
I Tried with below command
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
output
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
aaa 1
bbb 2
add a comment |
I Tried with below command
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
output
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
aaa 1
bbb 2
I Tried with below command
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
output
veen:~$ for i in aaa bbb; do perl -pne "s/n/ /g" i.txt| awk -v i="$i" 'print gsub(i,$0)';echo $i; done| sed "N;s/n/ /g"| awk 'print $2,$1'
aaa 1
bbb 2
answered yesterday
Praveen Kumar BSPraveen Kumar BS
1,6471311
1,6471311
add a comment |
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%2f507734%2fhow-to-grep-with-multiple-filters-and-print-one-by-one%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