You are not logged in.

#1 2022-11-30 21:21:45

xerxes_
Member
Registered: 2018-04-29
Posts: 675

[SOLVED] Multiline operations between 2 csv files in command line

I have a csv like file with numbers and strings separated by comas with lots of lines. I want to change third column (so it is between second and third coma) where are other numbers in each line by numbers from other file were are also different numbers in each lines but there is only one column of numbers.

Is it possible to do it with bash, sed, awk or something else what is able to run in command line?

Last edited by xerxes_ (2022-11-30 23:04:54)

Offline

#2 2022-11-30 21:57:44

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [SOLVED] Multiline operations between 2 csv files in command line

Use `cut` and `paste`, e.g.,

paste -d, <(cut -d, -f 1-2 first.csv) second.csv <(cut -d, -f 4- first.csv) > new.csv

If you don't mind the column order changing it's even easier (and doesn't require process substitution):

cut -d, -f 1-2,4- first.csv | paste -d, - second.csv > new.csv

(minor edit: s/first,csv/first.csv/)

Last edited by Trilby (2022-12-01 01:44:07)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2022-11-30 22:42:26

xerxes_
Member
Registered: 2018-04-29
Posts: 675

Re: [SOLVED] Multiline operations between 2 csv files in command line

No, the column order is important! Your first long command was almost good, but why after third column change the rest of the line is in new line?

UPDATE:
OK, now I know why is that: files were created on windows. Your solution is good. I'll mark it as solved, thanks.

Last edited by xerxes_ (2022-11-30 23:04:16)

Offline

#4 2022-12-01 01:42:39

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,523
Website

Re: [SOLVED] Multiline operations between 2 csv files in command line

xerxes_ wrote:

files were created on windows

Pretreat with the popular but totally unnecessary dos2unix, or with a `tr -d '\r'`.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB