Skip to content

Replace one column value by match on another column from a Key-Value 2nd file #972

Answered by aborruso
pjfarleyiii asked this question in Q&A
Discussion options

You must be logged in to vote

Please add always sample input and output files.

The input files

input.csv

id,Name,Category
1,a,7
2,b,9
3,c,2
4,a,7
5,b,9

value.csv

Name,Category
a,1
b,2

The steps

  • add progressive numeric column, to the input to have the original order
mlr --csv cat -n input.csv >tmp.csv
+---+----+------+----------+
| n | id | Name | Category |
+---+----+------+----------+
| 1 | 1  | a    | 7        |
| 2 | 2  | b    | 9        |
| 3 | 3  | c    | 2        |
| 4 | 4  | a    | 7        |
| 5 | 5  | b    | 9        |
+---+----+------+----------+
  • run a left join, creating a r_Category field, for the value.csv file
    • then if the right category is not empty $Category=$r_Category
    • then sort all, using the n

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
3 replies
@johnkerl
Comment options

@aborruso
Comment options

@johnkerl
Comment options

Answer selected by pjfarleyiii
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants