print entire line after an awk and grep
I need to search multiple patterns on the third column of a file, and then print the whole line.
I am using this one below but how do I get it to print the whole line where there is a match?
awk '{print $3}' file | egrep -w "S|M|D"
shell-script awk scripting
New contributor
add a comment |
I need to search multiple patterns on the third column of a file, and then print the whole line.
I am using this one below but how do I get it to print the whole line where there is a match?
awk '{print $3}' file | egrep -w "S|M|D"
shell-script awk scripting
New contributor
please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question
– Sundeep
2 days ago
add a comment |
I need to search multiple patterns on the third column of a file, and then print the whole line.
I am using this one below but how do I get it to print the whole line where there is a match?
awk '{print $3}' file | egrep -w "S|M|D"
shell-script awk scripting
New contributor
I need to search multiple patterns on the third column of a file, and then print the whole line.
I am using this one below but how do I get it to print the whole line where there is a match?
awk '{print $3}' file | egrep -w "S|M|D"
shell-script awk scripting
shell-script awk scripting
New contributor
New contributor
New contributor
asked 2 days ago
hayabusa99hayabusa99
31
31
New contributor
New contributor
please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question
– Sundeep
2 days ago
add a comment |
please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question
– Sundeep
2 days ago
please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question
– Sundeep
2 days ago
please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question
– Sundeep
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
I think your requirement just needs awk
and not a combination with grep
. If you are looking to print the whole line where the third column matches any of those letters, you need to do
awk '$3 ~ /^(S|M|D)$/' file
1
since it is single character, you can also use[SMD]
– Sundeep
2 days ago
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
2 days ago
It look like it works. Thanks!
– hayabusa99
2 days ago
add a comment |
To extract the lines whose 3rd whitespace-delimited field is exactly S
, M
or D
, use one of
awk '$3 ~ /^[SMD]$/' file
or, using string matching rather than regular expression matching,
awk '$3 == "S" || $3 == "M" || $3 == "D"' file
A condition without a corresponding block will act as if its block simply was { print }
.
add a comment |
awk '$3 ~ /^S/||/^M/||/^D/{print $0}' filename
The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.
– Jeff Schaller
8 hours ago
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
});
}
});
hayabusa99 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f492952%2fprint-entire-line-after-an-awk-and-grep%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
I think your requirement just needs awk
and not a combination with grep
. If you are looking to print the whole line where the third column matches any of those letters, you need to do
awk '$3 ~ /^(S|M|D)$/' file
1
since it is single character, you can also use[SMD]
– Sundeep
2 days ago
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
2 days ago
It look like it works. Thanks!
– hayabusa99
2 days ago
add a comment |
I think your requirement just needs awk
and not a combination with grep
. If you are looking to print the whole line where the third column matches any of those letters, you need to do
awk '$3 ~ /^(S|M|D)$/' file
1
since it is single character, you can also use[SMD]
– Sundeep
2 days ago
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
2 days ago
It look like it works. Thanks!
– hayabusa99
2 days ago
add a comment |
I think your requirement just needs awk
and not a combination with grep
. If you are looking to print the whole line where the third column matches any of those letters, you need to do
awk '$3 ~ /^(S|M|D)$/' file
I think your requirement just needs awk
and not a combination with grep
. If you are looking to print the whole line where the third column matches any of those letters, you need to do
awk '$3 ~ /^(S|M|D)$/' file
answered 2 days ago
InianInian
3,935824
3,935824
1
since it is single character, you can also use[SMD]
– Sundeep
2 days ago
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
2 days ago
It look like it works. Thanks!
– hayabusa99
2 days ago
add a comment |
1
since it is single character, you can also use[SMD]
– Sundeep
2 days ago
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
2 days ago
It look like it works. Thanks!
– hayabusa99
2 days ago
1
1
since it is single character, you can also use
[SMD]
– Sundeep
2 days ago
since it is single character, you can also use
[SMD]
– Sundeep
2 days ago
is this going to search for the exact match? like
grep -w
does?– hayabusa99
2 days ago
is this going to search for the exact match? like
grep -w
does?– hayabusa99
2 days ago
It look like it works. Thanks!
– hayabusa99
2 days ago
It look like it works. Thanks!
– hayabusa99
2 days ago
add a comment |
To extract the lines whose 3rd whitespace-delimited field is exactly S
, M
or D
, use one of
awk '$3 ~ /^[SMD]$/' file
or, using string matching rather than regular expression matching,
awk '$3 == "S" || $3 == "M" || $3 == "D"' file
A condition without a corresponding block will act as if its block simply was { print }
.
add a comment |
To extract the lines whose 3rd whitespace-delimited field is exactly S
, M
or D
, use one of
awk '$3 ~ /^[SMD]$/' file
or, using string matching rather than regular expression matching,
awk '$3 == "S" || $3 == "M" || $3 == "D"' file
A condition without a corresponding block will act as if its block simply was { print }
.
add a comment |
To extract the lines whose 3rd whitespace-delimited field is exactly S
, M
or D
, use one of
awk '$3 ~ /^[SMD]$/' file
or, using string matching rather than regular expression matching,
awk '$3 == "S" || $3 == "M" || $3 == "D"' file
A condition without a corresponding block will act as if its block simply was { print }
.
To extract the lines whose 3rd whitespace-delimited field is exactly S
, M
or D
, use one of
awk '$3 ~ /^[SMD]$/' file
or, using string matching rather than regular expression matching,
awk '$3 == "S" || $3 == "M" || $3 == "D"' file
A condition without a corresponding block will act as if its block simply was { print }
.
answered 2 days ago
KusalanandaKusalananda
123k16232379
123k16232379
add a comment |
add a comment |
awk '$3 ~ /^S/||/^M/||/^D/{print $0}' filename
The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.
– Jeff Schaller
8 hours ago
add a comment |
awk '$3 ~ /^S/||/^M/||/^D/{print $0}' filename
The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.
– Jeff Schaller
8 hours ago
add a comment |
awk '$3 ~ /^S/||/^M/||/^D/{print $0}' filename
awk '$3 ~ /^S/||/^M/||/^D/{print $0}' filename
answered 10 hours ago
Praveen Kumar BSPraveen Kumar BS
1,248138
1,248138
The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.
– Jeff Schaller
8 hours ago
add a comment |
The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.
– Jeff Schaller
8 hours ago
The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.
– Jeff Schaller
8 hours ago
The way this parses out will match column 3 starting with an S, or column 1 starting with an M or D -- it misses column 3 M/D and it incorrectly includes the column 1 matches.
– Jeff Schaller
8 hours ago
add a comment |
hayabusa99 is a new contributor. Be nice, and check out our Code of Conduct.
hayabusa99 is a new contributor. Be nice, and check out our Code of Conduct.
hayabusa99 is a new contributor. Be nice, and check out our Code of Conduct.
hayabusa99 is a new contributor. Be nice, and check out our Code of Conduct.
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.
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%2funix.stackexchange.com%2fquestions%2f492952%2fprint-entire-line-after-an-awk-and-grep%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
please click edit and add few lines of input and expected output - some line should match and others shouldn't... this will help to add clarity to question as well as act as test data for those who wish to answer your question
– Sundeep
2 days ago