print entire line after an awk and grep
Clash Royale CLAN TAG#URR8PPP
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
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
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
Jan 7 at 9:23
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
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
asked Jan 7 at 9:17
hayabusa99hayabusa99
31
31
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
Jan 7 at 9:23
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
Jan 7 at 9:23
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
Jan 7 at 9:23
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
Jan 7 at 9:23
add a comment |
4 Answers
4
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
2
since it is single character, you can also use[SMD]
– Sundeep
Jan 7 at 9:28
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
Jan 7 at 9:33
It look like it works. Thanks!
– hayabusa99
Jan 7 at 9:34
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
Jan 9 at 19:52
add a comment |
Same has been achieved in python and it worked fine
Code
#!/usr/bin/python
import subprocess
import re
h=re.compile(r'^[SMD]')
o=open('l.txt','r')
for i in o:
j=i.split(':')
if re.search(h,j[2]):
print i.strip()
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%2f492952%2fprint-entire-line-after-an-awk-and-grep%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
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
2
since it is single character, you can also use[SMD]
– Sundeep
Jan 7 at 9:28
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
Jan 7 at 9:33
It look like it works. Thanks!
– hayabusa99
Jan 7 at 9:34
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
2
since it is single character, you can also use[SMD]
– Sundeep
Jan 7 at 9:28
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
Jan 7 at 9:33
It look like it works. Thanks!
– hayabusa99
Jan 7 at 9:34
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 Jan 7 at 9:27
InianInian
3,945824
3,945824
2
since it is single character, you can also use[SMD]
– Sundeep
Jan 7 at 9:28
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
Jan 7 at 9:33
It look like it works. Thanks!
– hayabusa99
Jan 7 at 9:34
add a comment |
2
since it is single character, you can also use[SMD]
– Sundeep
Jan 7 at 9:28
is this going to search for the exact match? likegrep -w
does?
– hayabusa99
Jan 7 at 9:33
It look like it works. Thanks!
– hayabusa99
Jan 7 at 9:34
2
2
since it is single character, you can also use
[SMD]
– Sundeep
Jan 7 at 9:28
since it is single character, you can also use
[SMD]
– Sundeep
Jan 7 at 9:28
is this going to search for the exact match? like
grep -w
does?– hayabusa99
Jan 7 at 9:33
is this going to search for the exact match? like
grep -w
does?– hayabusa99
Jan 7 at 9:33
It look like it works. Thanks!
– hayabusa99
Jan 7 at 9:34
It look like it works. Thanks!
– hayabusa99
Jan 7 at 9:34
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 Jan 7 at 9:37
KusalanandaKusalananda
125k16236389
125k16236389
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
Jan 9 at 19:52
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
Jan 9 at 19:52
add a comment |
awk '$3 ~ /^S/||/^M/||/^D/print $0' filename
awk '$3 ~ /^S/||/^M/||/^D/print $0' filename
answered Jan 9 at 17:56
Praveen Kumar BSPraveen Kumar BS
1,346138
1,346138
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
Jan 9 at 19:52
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
Jan 9 at 19:52
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
Jan 9 at 19:52
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
Jan 9 at 19:52
add a comment |
Same has been achieved in python and it worked fine
Code
#!/usr/bin/python
import subprocess
import re
h=re.compile(r'^[SMD]')
o=open('l.txt','r')
for i in o:
j=i.split(':')
if re.search(h,j[2]):
print i.strip()
add a comment |
Same has been achieved in python and it worked fine
Code
#!/usr/bin/python
import subprocess
import re
h=re.compile(r'^[SMD]')
o=open('l.txt','r')
for i in o:
j=i.split(':')
if re.search(h,j[2]):
print i.strip()
add a comment |
Same has been achieved in python and it worked fine
Code
#!/usr/bin/python
import subprocess
import re
h=re.compile(r'^[SMD]')
o=open('l.txt','r')
for i in o:
j=i.split(':')
if re.search(h,j[2]):
print i.strip()
Same has been achieved in python and it worked fine
Code
#!/usr/bin/python
import subprocess
import re
h=re.compile(r'^[SMD]')
o=open('l.txt','r')
for i in o:
j=i.split(':')
if re.search(h,j[2]):
print i.strip()
answered Jan 13 at 6:23
Praveen Kumar BSPraveen Kumar BS
1,346138
1,346138
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%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
Jan 7 at 9:23