Retrieve first occurrence of record, where matching pattern is taken from input

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a list like this:
2017-12-11 AAOI 40.33
2017-11-15 AAOI 44.3492
2017-12-15 AEIS 70.98
2017-11-15 AEIS 80.137
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-12-12 BLOC 2.7
2017-12-11 BLOC 2.32
2017-12-04 BLOC 2.39
2017-11-27 BLOC 2.6
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
etc.
I want to get the first (most rescent) match for each symbol, symbol held in second column. With the sample input above this should be the output:
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
The list is already sorted by column 2 ascending, then column 1 descending.
I am thinking along the lines of using awk to set the matching pattern to $2 (second column) and pipe matches based on this pattern into head.
This is not the first unique occurrence; it is the first unique occurrence where uniqueness is based on column 2 only. Like a uniq by column and return first occurrence only. Accordingly generous with the tags.
I fail connecting the dots. How would you do it?
text-processing awk sed
add a comment |Â
up vote
0
down vote
favorite
I have a list like this:
2017-12-11 AAOI 40.33
2017-11-15 AAOI 44.3492
2017-12-15 AEIS 70.98
2017-11-15 AEIS 80.137
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-12-12 BLOC 2.7
2017-12-11 BLOC 2.32
2017-12-04 BLOC 2.39
2017-11-27 BLOC 2.6
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
etc.
I want to get the first (most rescent) match for each symbol, symbol held in second column. With the sample input above this should be the output:
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
The list is already sorted by column 2 ascending, then column 1 descending.
I am thinking along the lines of using awk to set the matching pattern to $2 (second column) and pipe matches based on this pattern into head.
This is not the first unique occurrence; it is the first unique occurrence where uniqueness is based on column 2 only. Like a uniq by column and return first occurrence only. Accordingly generous with the tags.
I fail connecting the dots. How would you do it?
text-processing awk sed
2
awk '!seen[$2]++' list_file
â don_crissti
Dec 26 '17 at 0:06
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a list like this:
2017-12-11 AAOI 40.33
2017-11-15 AAOI 44.3492
2017-12-15 AEIS 70.98
2017-11-15 AEIS 80.137
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-12-12 BLOC 2.7
2017-12-11 BLOC 2.32
2017-12-04 BLOC 2.39
2017-11-27 BLOC 2.6
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
etc.
I want to get the first (most rescent) match for each symbol, symbol held in second column. With the sample input above this should be the output:
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
The list is already sorted by column 2 ascending, then column 1 descending.
I am thinking along the lines of using awk to set the matching pattern to $2 (second column) and pipe matches based on this pattern into head.
This is not the first unique occurrence; it is the first unique occurrence where uniqueness is based on column 2 only. Like a uniq by column and return first occurrence only. Accordingly generous with the tags.
I fail connecting the dots. How would you do it?
text-processing awk sed
I have a list like this:
2017-12-11 AAOI 40.33
2017-11-15 AAOI 44.3492
2017-12-15 AEIS 70.98
2017-11-15 AEIS 80.137
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-12-12 BLOC 2.7
2017-12-11 BLOC 2.32
2017-12-04 BLOC 2.39
2017-11-27 BLOC 2.6
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
etc.
I want to get the first (most rescent) match for each symbol, symbol held in second column. With the sample input above this should be the output:
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
The list is already sorted by column 2 ascending, then column 1 descending.
I am thinking along the lines of using awk to set the matching pattern to $2 (second column) and pipe matches based on this pattern into head.
This is not the first unique occurrence; it is the first unique occurrence where uniqueness is based on column 2 only. Like a uniq by column and return first occurrence only. Accordingly generous with the tags.
I fail connecting the dots. How would you do it?
text-processing awk sed
edited Dec 26 '17 at 2:19
Jeff Schaller
31.8k848109
31.8k848109
asked Dec 26 '17 at 0:00
HenrikJson
257
257
2
awk '!seen[$2]++' list_file
â don_crissti
Dec 26 '17 at 0:06
add a comment |Â
2
awk '!seen[$2]++' list_file
â don_crissti
Dec 26 '17 at 0:06
2
2
awk '!seen[$2]++' list_fileâ don_crissti
Dec 26 '17 at 0:06
awk '!seen[$2]++' list_fileâ don_crissti
Dec 26 '17 at 0:06
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
Two ways to do it:
sort sort -u -k2,2 infile
awk awk -F" " '!_[$2]++' infile
add a comment |Â
up vote
0
down vote
I have done this by awk and sed combination.
for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done
output
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
Have a UUoC award
â Fox
Dec 26 '17 at 10:50
@Fox didnt get ?
â Praveen Kumar BS
Dec 26 '17 at 16:26
There's no need forcathere, both because shell redirection exists and becauseawktakes a filename argument
â Fox
Dec 26 '17 at 16:30
yes agreed @Fox
â Praveen Kumar BS
Dec 26 '17 at 16:32
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Two ways to do it:
sort sort -u -k2,2 infile
awk awk -F" " '!_[$2]++' infile
add a comment |Â
up vote
2
down vote
Two ways to do it:
sort sort -u -k2,2 infile
awk awk -F" " '!_[$2]++' infile
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Two ways to do it:
sort sort -u -k2,2 infile
awk awk -F" " '!_[$2]++' infile
Two ways to do it:
sort sort -u -k2,2 infile
awk awk -F" " '!_[$2]++' infile
answered Dec 26 '17 at 0:20
Isaac
6,7911834
6,7911834
add a comment |Â
add a comment |Â
up vote
0
down vote
I have done this by awk and sed combination.
for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done
output
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
Have a UUoC award
â Fox
Dec 26 '17 at 10:50
@Fox didnt get ?
â Praveen Kumar BS
Dec 26 '17 at 16:26
There's no need forcathere, both because shell redirection exists and becauseawktakes a filename argument
â Fox
Dec 26 '17 at 16:30
yes agreed @Fox
â Praveen Kumar BS
Dec 26 '17 at 16:32
add a comment |Â
up vote
0
down vote
I have done this by awk and sed combination.
for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done
output
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
Have a UUoC award
â Fox
Dec 26 '17 at 10:50
@Fox didnt get ?
â Praveen Kumar BS
Dec 26 '17 at 16:26
There's no need forcathere, both because shell redirection exists and becauseawktakes a filename argument
â Fox
Dec 26 '17 at 16:30
yes agreed @Fox
â Praveen Kumar BS
Dec 26 '17 at 16:32
add a comment |Â
up vote
0
down vote
up vote
0
down vote
I have done this by awk and sed combination.
for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done
output
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
I have done this by awk and sed combination.
for w in `cat filename | awk 'print $2' | sort | uniq`; do sed -n '/'$w'/p' filename| sed -n '1p'; done
output
2017-12-11 AAOI 40.33
2017-12-15 AEIS 70.98
2017-10-23 AIEQ 25.1601
2017-11-15 AMBA 52.6501
2017-12-05 ATHM 57.2
2017-11-09 AUDC 7.02
2017-12-22 BEW 0.58
2017-10-17 BIOP 8.19
2017-12-08 BLDP 4.86
2017-12-21 BLOC 2.3
2017-11-15 BOX 21.63
2017-12-22 BTL 10.5638
answered Dec 26 '17 at 5:21
Praveen Kumar BS
1,010128
1,010128
Have a UUoC award
â Fox
Dec 26 '17 at 10:50
@Fox didnt get ?
â Praveen Kumar BS
Dec 26 '17 at 16:26
There's no need forcathere, both because shell redirection exists and becauseawktakes a filename argument
â Fox
Dec 26 '17 at 16:30
yes agreed @Fox
â Praveen Kumar BS
Dec 26 '17 at 16:32
add a comment |Â
Have a UUoC award
â Fox
Dec 26 '17 at 10:50
@Fox didnt get ?
â Praveen Kumar BS
Dec 26 '17 at 16:26
There's no need forcathere, both because shell redirection exists and becauseawktakes a filename argument
â Fox
Dec 26 '17 at 16:30
yes agreed @Fox
â Praveen Kumar BS
Dec 26 '17 at 16:32
Have a UUoC award
â Fox
Dec 26 '17 at 10:50
Have a UUoC award
â Fox
Dec 26 '17 at 10:50
@Fox didnt get ?
â Praveen Kumar BS
Dec 26 '17 at 16:26
@Fox didnt get ?
â Praveen Kumar BS
Dec 26 '17 at 16:26
There's no need for
cat here, both because shell redirection exists and because awk takes a filename argumentâ Fox
Dec 26 '17 at 16:30
There's no need for
cat here, both because shell redirection exists and because awk takes a filename argumentâ Fox
Dec 26 '17 at 16:30
yes agreed @Fox
â Praveen Kumar BS
Dec 26 '17 at 16:32
yes agreed @Fox
â Praveen Kumar BS
Dec 26 '17 at 16:32
add a comment |Â
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%2f413038%2fretrieve-first-occurrence-of-record-where-matching-pattern-is-taken-from-input%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
2
awk '!seen[$2]++' list_fileâ don_crissti
Dec 26 '17 at 0:06