S
S
Stanislav Martynov2018-08-17 17:12:10
linux
Stanislav Martynov, 2018-08-17 17:12:10

bash. How to sort csv file in bash?

Good time.
There is a file.csv file like this:
"FIELD_1, 2", 2
"FIELD_1",1
"FIELD_1, 2, 3", 3
If you run
sort --field-separator="," -k2 -n file.csv > sorted .csv
In theory, sorted.csv should contain sorted data by the 2nd field, but this does not work because the quotes contain commas. How can this be resolved.
As a result, I want to get the file
"FIELD_1",1
"FIELD_1, 2", 2
"FIELD_1, 2, 3", 3
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Saboteur, 2018-08-17
@Win332

so simple and sort by the second field:
sort -k2 -n file.csv > sorted.csv

S
SOTVM, 2018-08-17
@sotvm

Yes, everything seems to work - I tried it (I'm learning myself), I didn't even write the code, your copy / paste-silt.
what is contained in quotes "" is read as a single string/character. -FALSE is
the only thing / maybe the csv itself needs to be split into lines beforehand,
or does each FILD value / parameter start on a new line?
then it should work

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question