Extract first column value for line with specific second column value from three-columns file with awk
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
I need to Write an awk
command that will return the identification number from the following table for only the lines where the title is Turtle
. This table is stored in turtle.txt
Id Num. Title CatchDate
433417 RedTurtle 2001-06-29
493303 BlueTurtle 1998-09-20
259497 Turtle 1985-05-08
229505 RedTurtle 1994-07-13
473076 OrangeTurtle 2002-03-08
221907 Blueturtle 1999-07-02
457032 Turtle 1993-04-09
490359 RedTurtle 1996-11-12
494595 SnappingTurtle 1985-05-20
402421 BlueTurtle 1999-08-16
text-processing awk
New contributor
add a comment |Â
up vote
5
down vote
favorite
I need to Write an awk
command that will return the identification number from the following table for only the lines where the title is Turtle
. This table is stored in turtle.txt
Id Num. Title CatchDate
433417 RedTurtle 2001-06-29
493303 BlueTurtle 1998-09-20
259497 Turtle 1985-05-08
229505 RedTurtle 1994-07-13
473076 OrangeTurtle 2002-03-08
221907 Blueturtle 1999-07-02
457032 Turtle 1993-04-09
490359 RedTurtle 1996-11-12
494595 SnappingTurtle 1985-05-20
402421 BlueTurtle 1999-08-16
text-processing awk
New contributor
1
I'm amazed that this simple question could attract 6 answers so far.
â simlev
1 hour ago
@simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
â Jeff Schaller
1 hour ago
Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
â Michael Kjörling
19 secs ago
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I need to Write an awk
command that will return the identification number from the following table for only the lines where the title is Turtle
. This table is stored in turtle.txt
Id Num. Title CatchDate
433417 RedTurtle 2001-06-29
493303 BlueTurtle 1998-09-20
259497 Turtle 1985-05-08
229505 RedTurtle 1994-07-13
473076 OrangeTurtle 2002-03-08
221907 Blueturtle 1999-07-02
457032 Turtle 1993-04-09
490359 RedTurtle 1996-11-12
494595 SnappingTurtle 1985-05-20
402421 BlueTurtle 1999-08-16
text-processing awk
New contributor
I need to Write an awk
command that will return the identification number from the following table for only the lines where the title is Turtle
. This table is stored in turtle.txt
Id Num. Title CatchDate
433417 RedTurtle 2001-06-29
493303 BlueTurtle 1998-09-20
259497 Turtle 1985-05-08
229505 RedTurtle 1994-07-13
473076 OrangeTurtle 2002-03-08
221907 Blueturtle 1999-07-02
457032 Turtle 1993-04-09
490359 RedTurtle 1996-11-12
494595 SnappingTurtle 1985-05-20
402421 BlueTurtle 1999-08-16
text-processing awk
text-processing awk
New contributor
New contributor
edited 1 min ago
Michael Kjörling
16.2k84799
16.2k84799
New contributor
asked 4 hours ago
Kamat
333
333
New contributor
New contributor
1
I'm amazed that this simple question could attract 6 answers so far.
â simlev
1 hour ago
@simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
â Jeff Schaller
1 hour ago
Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
â Michael Kjörling
19 secs ago
add a comment |Â
1
I'm amazed that this simple question could attract 6 answers so far.
â simlev
1 hour ago
@simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
â Jeff Schaller
1 hour ago
Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
â Michael Kjörling
19 secs ago
1
1
I'm amazed that this simple question could attract 6 answers so far.
â simlev
1 hour ago
I'm amazed that this simple question could attract 6 answers so far.
â simlev
1 hour ago
@simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
â Jeff Schaller
1 hour ago
@simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
â Jeff Schaller
1 hour ago
Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
â Michael Kjörling
19 secs ago
Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
â Michael Kjörling
19 secs ago
add a comment |Â
6 Answers
6
active
oldest
votes
up vote
5
down vote
You can use:
awk '$2 == "Turtle" print $1' file
259497
457032
add a comment |Â
up vote
5
down vote
awk '$2 == "Turtle" print $1' turtle.txt
259497
457032
$2 is the field to select
Turtle is the text to match
print $1 is to print the first field.
turtle.txt is the name of the source file.
add a comment |Â
up vote
4
down vote
Golfing it:
$ awk '$2=="Turtle"&&$0=$1' <file
259497
457032
Or, expanded in stages until we reach Isaac's and Goro's answers
awk '$2 == "Turtle" && $0 = $1' <file
awk '$2 == "Turtle" $0 = $1; print ' <file
awk '$2 == "Turtle" print $1 ' <file
The three are not exactly equivalent as my golfed code would not print the number if it was zero (the result of $0=$1
is used as a conditional).
Here's a proper sed
solution to make up for the golfing above:
$ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
259497
457032
It finds all lines containing the word Turtle
and then remowes the first space or tab character and everything after it on those lines before printing them (printing of other lines is inhibited by -n
).
The <
and >
matches word start and end boundaries so that <Turtle>
matches only the string Turtle
and not e.g. RedTurtle
.
The first one won't work properly when the id is zero
â user000001
1 hour ago
@user000001 This was already made clear in the answer.
â Kusalananda
1 hour ago
1
But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions inawk
).
â Stéphane Chazelas
1 hour ago
Note that<Turtle>
is not portable and would match on Red-Turtle. You would need something likesed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p'
for a real equivalent.
â Stéphane Chazelas
1 hour ago
add a comment |Â
up vote
3
down vote
non-awk alternative:
grep -w "Turtle" turtle.txt | cut -d " " -f 1
add a comment |Â
up vote
3
down vote
Using sed:
sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file
add a comment |Â
up vote
1
down vote
You may employ grep
in this:
grep -oP '^d+(?=h+Turtleh)'
add a comment |Â
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
You can use:
awk '$2 == "Turtle" print $1' file
259497
457032
add a comment |Â
up vote
5
down vote
You can use:
awk '$2 == "Turtle" print $1' file
259497
457032
add a comment |Â
up vote
5
down vote
up vote
5
down vote
You can use:
awk '$2 == "Turtle" print $1' file
259497
457032
You can use:
awk '$2 == "Turtle" print $1' file
259497
457032
edited 4 hours ago
answered 4 hours ago
Goro
7,54253270
7,54253270
add a comment |Â
add a comment |Â
up vote
5
down vote
awk '$2 == "Turtle" print $1' turtle.txt
259497
457032
$2 is the field to select
Turtle is the text to match
print $1 is to print the first field.
turtle.txt is the name of the source file.
add a comment |Â
up vote
5
down vote
awk '$2 == "Turtle" print $1' turtle.txt
259497
457032
$2 is the field to select
Turtle is the text to match
print $1 is to print the first field.
turtle.txt is the name of the source file.
add a comment |Â
up vote
5
down vote
up vote
5
down vote
awk '$2 == "Turtle" print $1' turtle.txt
259497
457032
$2 is the field to select
Turtle is the text to match
print $1 is to print the first field.
turtle.txt is the name of the source file.
awk '$2 == "Turtle" print $1' turtle.txt
259497
457032
$2 is the field to select
Turtle is the text to match
print $1 is to print the first field.
turtle.txt is the name of the source file.
edited 4 hours ago
answered 4 hours ago
Isaac
7,81711137
7,81711137
add a comment |Â
add a comment |Â
up vote
4
down vote
Golfing it:
$ awk '$2=="Turtle"&&$0=$1' <file
259497
457032
Or, expanded in stages until we reach Isaac's and Goro's answers
awk '$2 == "Turtle" && $0 = $1' <file
awk '$2 == "Turtle" $0 = $1; print ' <file
awk '$2 == "Turtle" print $1 ' <file
The three are not exactly equivalent as my golfed code would not print the number if it was zero (the result of $0=$1
is used as a conditional).
Here's a proper sed
solution to make up for the golfing above:
$ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
259497
457032
It finds all lines containing the word Turtle
and then remowes the first space or tab character and everything after it on those lines before printing them (printing of other lines is inhibited by -n
).
The <
and >
matches word start and end boundaries so that <Turtle>
matches only the string Turtle
and not e.g. RedTurtle
.
The first one won't work properly when the id is zero
â user000001
1 hour ago
@user000001 This was already made clear in the answer.
â Kusalananda
1 hour ago
1
But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions inawk
).
â Stéphane Chazelas
1 hour ago
Note that<Turtle>
is not portable and would match on Red-Turtle. You would need something likesed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p'
for a real equivalent.
â Stéphane Chazelas
1 hour ago
add a comment |Â
up vote
4
down vote
Golfing it:
$ awk '$2=="Turtle"&&$0=$1' <file
259497
457032
Or, expanded in stages until we reach Isaac's and Goro's answers
awk '$2 == "Turtle" && $0 = $1' <file
awk '$2 == "Turtle" $0 = $1; print ' <file
awk '$2 == "Turtle" print $1 ' <file
The three are not exactly equivalent as my golfed code would not print the number if it was zero (the result of $0=$1
is used as a conditional).
Here's a proper sed
solution to make up for the golfing above:
$ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
259497
457032
It finds all lines containing the word Turtle
and then remowes the first space or tab character and everything after it on those lines before printing them (printing of other lines is inhibited by -n
).
The <
and >
matches word start and end boundaries so that <Turtle>
matches only the string Turtle
and not e.g. RedTurtle
.
The first one won't work properly when the id is zero
â user000001
1 hour ago
@user000001 This was already made clear in the answer.
â Kusalananda
1 hour ago
1
But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions inawk
).
â Stéphane Chazelas
1 hour ago
Note that<Turtle>
is not portable and would match on Red-Turtle. You would need something likesed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p'
for a real equivalent.
â Stéphane Chazelas
1 hour ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Golfing it:
$ awk '$2=="Turtle"&&$0=$1' <file
259497
457032
Or, expanded in stages until we reach Isaac's and Goro's answers
awk '$2 == "Turtle" && $0 = $1' <file
awk '$2 == "Turtle" $0 = $1; print ' <file
awk '$2 == "Turtle" print $1 ' <file
The three are not exactly equivalent as my golfed code would not print the number if it was zero (the result of $0=$1
is used as a conditional).
Here's a proper sed
solution to make up for the golfing above:
$ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
259497
457032
It finds all lines containing the word Turtle
and then remowes the first space or tab character and everything after it on those lines before printing them (printing of other lines is inhibited by -n
).
The <
and >
matches word start and end boundaries so that <Turtle>
matches only the string Turtle
and not e.g. RedTurtle
.
Golfing it:
$ awk '$2=="Turtle"&&$0=$1' <file
259497
457032
Or, expanded in stages until we reach Isaac's and Goro's answers
awk '$2 == "Turtle" && $0 = $1' <file
awk '$2 == "Turtle" $0 = $1; print ' <file
awk '$2 == "Turtle" print $1 ' <file
The three are not exactly equivalent as my golfed code would not print the number if it was zero (the result of $0=$1
is used as a conditional).
Here's a proper sed
solution to make up for the golfing above:
$ sed -n '/<Turtle>/s/[[:blank:]].*//p' <file
259497
457032
It finds all lines containing the word Turtle
and then remowes the first space or tab character and everything after it on those lines before printing them (printing of other lines is inhibited by -n
).
The <
and >
matches word start and end boundaries so that <Turtle>
matches only the string Turtle
and not e.g. RedTurtle
.
edited 3 hours ago
answered 4 hours ago
Kusalananda
109k14210333
109k14210333
The first one won't work properly when the id is zero
â user000001
1 hour ago
@user000001 This was already made clear in the answer.
â Kusalananda
1 hour ago
1
But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions inawk
).
â Stéphane Chazelas
1 hour ago
Note that<Turtle>
is not portable and would match on Red-Turtle. You would need something likesed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p'
for a real equivalent.
â Stéphane Chazelas
1 hour ago
add a comment |Â
The first one won't work properly when the id is zero
â user000001
1 hour ago
@user000001 This was already made clear in the answer.
â Kusalananda
1 hour ago
1
But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions inawk
).
â Stéphane Chazelas
1 hour ago
Note that<Turtle>
is not portable and would match on Red-Turtle. You would need something likesed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p'
for a real equivalent.
â Stéphane Chazelas
1 hour ago
The first one won't work properly when the id is zero
â user000001
1 hour ago
The first one won't work properly when the id is zero
â user000001
1 hour ago
@user000001 This was already made clear in the answer.
â Kusalananda
1 hour ago
@user000001 This was already made clear in the answer.
â Kusalananda
1 hour ago
1
1
But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in
awk
).â Stéphane Chazelas
1 hour ago
But it adds nothing useful IMO. That's more of an example of things not to do (use assignments as conditions in
awk
).â Stéphane Chazelas
1 hour ago
Note that
<Turtle>
is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p'
for a real equivalent.â Stéphane Chazelas
1 hour ago
Note that
<Turtle>
is not portable and would match on Red-Turtle. You would need something like sed -n 's/^[[:blank:]]*([^[:blank:]]1,)[[:blank:]]1,Turtle([[:blank:]].*)0,1$/1/p'
for a real equivalent.â Stéphane Chazelas
1 hour ago
add a comment |Â
up vote
3
down vote
non-awk alternative:
grep -w "Turtle" turtle.txt | cut -d " " -f 1
add a comment |Â
up vote
3
down vote
non-awk alternative:
grep -w "Turtle" turtle.txt | cut -d " " -f 1
add a comment |Â
up vote
3
down vote
up vote
3
down vote
non-awk alternative:
grep -w "Turtle" turtle.txt | cut -d " " -f 1
non-awk alternative:
grep -w "Turtle" turtle.txt | cut -d " " -f 1
answered 4 hours ago
RobotJohnny
740216
740216
add a comment |Â
add a comment |Â
up vote
3
down vote
Using sed:
sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file
add a comment |Â
up vote
3
down vote
Using sed:
sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Using sed:
sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file
Using sed:
sed -n '/sTurtles/s/^([0-9]+)s.*/1/p' file
answered 4 hours ago
oliv
1,121210
1,121210
add a comment |Â
add a comment |Â
up vote
1
down vote
You may employ grep
in this:
grep -oP '^d+(?=h+Turtleh)'
add a comment |Â
up vote
1
down vote
You may employ grep
in this:
grep -oP '^d+(?=h+Turtleh)'
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You may employ grep
in this:
grep -oP '^d+(?=h+Turtleh)'
You may employ grep
in this:
grep -oP '^d+(?=h+Turtleh)'
answered 1 hour ago
Rakesh Sharma
207113
207113
add a comment |Â
add a comment |Â
Kamat is a new contributor. Be nice, and check out our Code of Conduct.
Kamat is a new contributor. Be nice, and check out our Code of Conduct.
Kamat is a new contributor. Be nice, and check out our Code of Conduct.
Kamat 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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f473978%2fextract-first-column-value-for-line-with-specific-second-column-value-from-three%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
I'm amazed that this simple question could attract 6 answers so far.
â simlev
1 hour ago
@simlev, the Hot Network Questions algorithm is a mystery; it's contributing to the views & votes here
â Jeff Schaller
1 hour ago
Hi Kamat. I generalized the title of your question. The answer would be the same if this was about, say, a part number / description / price table, so the fact that it's about animals in this case seems inconsequential to answering the question.
â Michael Kjörling
19 secs ago