Using Raku (formerly known as Perl_6)
~$ raku -ne 'BEGIN my @a = "nbr_list.txt".IO.lines.map: *.Int; \
.put if $_.split(",")[0] == any(@a);' file.csv
#OR
~$ raku -ne 'BEGIN my @a = "nbr_list.txt".IO.lines.map: *.Int; \
.put if $_.split(",")[0] ~~ any(@a);' file.csv
Raku is a programming language in the Perl-family. As you BEGIN
, take the list of numbers and store in an array, @a
. The -ne
flags then reads files off the command line without auto-printing (awk
-like behavior).
Here, the line is read into topic variable ($_
), which is split
on commas, and the first element ([0]
) is taken. These elements are compared using the ==
numerical equality operator (first code example), or Raku's ~~
smart-matching operator. On the RHS of the operator, the @a
array is turned into an any()
junction. The if
conditional output
s the line if the condition is satisfied.
Sample Input:
1,Michael Jordan
2,Karl Malone,
3,Charles Barkley
4,Greg Anthony
5,Chris Mullen
6,Reggie Miller
7,Billy Owens
8,David Robinson
9,Shaquille O'Neal
10,John Stockton
Sample Output (using a nbr_list.txt
file consisting of 1,4,7,9):
1,Michael Jordan
4,Greg Anthony
7,Billy Owens
9,Shaquille O'Neal
Junctions are interesting because they auto-thread. For the problem stated above, a one()
junction also works, and might even be more efficient.
An alternative approach uses Sets: the line-numbers are turned into a Set
of Int
s. Data is read in linewise, each first column is coerced to an Int
, and checked to see whether it is an (elem)
element of the set. Note, either infix (elem)
or infix ∈
, (the Unicode symbol) can be used in the code below:
~$ raku -ne 'BEGIN my $set1 = Set.new("nbr_list.txt".IO.lines.map: *.Int); \
.put if $_.split(",").[0].Int (elem) $set1;' file
Since Raku Sets can only contain unique values (i.e. they "unique-ify" input), the second example will discard duplicates in the "nbr_list.txt"
file. Which indeed may be what the OP desires.
https://docs.raku.org/type/Junction
https://docs-stage.raku.org/type/Junction
https://raku.org