Find value from one csv in another one (like vlookup) in bash (Linux)

前端 未结 4 1941
猫巷女王i
猫巷女王i 2021-01-03 16:38

I have already tried all options that I found online to solve my issue but without good result.

Basically I have two csv files (pipe separated):

file1.

4条回答
  •  执笔经年
    2021-01-03 17:10

    You can use Miller (https://github.com/johnkerl/miller).

    Starting from input01.txt

    123|21|0452|IE|IE|1|MAYOBAN|BRIN|OFFICE|STREET|MAIN STREET|MAYOBAN|
    123|21|0453|IE|IE|1|CORKKIN|ROBERT|SURNAME|CORK|APTS|CORKKIN|
    123|21|0452|IE|IE|1|CORKCOR|NAME|HARRINGTON|DUBLIN|STREET|CORKCOR|
    

    and input02.txt

    MAYOBAN|BANGOR|2400
    MAYOBEL|BELLAVARY|2400
    CORKKIN|KINSALE|2200
    CORKCOR|CORK|2200
    DUBLD11|DUBLIN 11|2100
    

    and running

    mlr --csv -N --ifs "|" join  -j 7 -l 7 -r 1 -f input01.txt then cut -f 3 input02.txt
    

    you will have

    2400
    2200
    2200
    

    Some notes:

    • -N to set input and output without header;
    • --ifs "|" to set the input fields separator;
    • -l 7 -r 1 to set the join fields of the input files;
    • cut -f 3 to extract the field named 3 from the join output

提交回复
热议问题