0

I'm trying to rearrange the columns using the awk command.

Whenever the cell of csv contains commas ex: (Ok, this is an example, thanks). Then awk creates more columns as it sees the commas in the cell. Due to which csv format is getting messed up.

So how can we ignore the delimiter while rearranging the columns in a csv?

command that I'm using is,

awk 'BEGIN {FS=","; OFS=","} {print "$4", "$2", "$3", "$1"}' ols.csv > rearranged.csv
  • If you want an awk solution then see [whats-the-most-robust-way-to-efficiently-parse-csv-using-awk](https://stackoverflow.com/questions/45420535/whats-the-most-robust-way-to-efficiently-parse-csv-using-awk). – Ed Morton Mar 29 '22 at 14:42

2 Answers2

1

Using the

$ cat file.csv
first,"the second","third, this is","fourth column"

$ csvcut -c 4,2,3,1 file.csv
fourth column,the second,"third, this is",first

Or

$ mlr -N --icsv --ocsv cut -of 4,2,3,1 file.csv
fourth column,the second,"third, this is",first

Or

$ ruby -rcsv -e 'CSV.foreach(ARGV.shift) do |row|
    puts CSV.generate_line([row[3], row[1], row[2], row[0]])
  end' file.csv
fourth column,the second,"third, this is",first
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

If you have quoted CSV then you might use GNU AWK with FPAT suggested in GNU AWK manual that is FPAT="([^,]*)|(\"[^\"]+\")". Simple example, let file.csv content be

"uno","dos,dos","tres,tres,tres"

thenk

awk 'BEGIN{FPAT="([^,]*)|(\"[^\"]+\")";OFS=","}{print $1,$3,$2}' file.csv

output

"uno","tres,tres,tres","dos,dos"

Explanation: inform GNU AWK using field pattern (FPAT) that column is zero or more (*) repetitions of not-commas ([^,]) or (|) " followed by 1 or more (+) not-" ([^\"]) followed by ". Note that " must be escaped os otherwise they would be interpreted as string termination.

(tested in gawk 4.2.1)

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • That wouldn't allow nested quotes or empty quoted fields, see [whats-the-most-robust-way-to-efficiently-parse-csv-using-awk](https://stackoverflow.com/questions/45420535/whats-the-most-robust-way-to-efficiently-parse-csv-using-awk) and https://www.gnu.org/software/gawk/manual/gawk.html#More-CSV for a more robust `FPAT` definition. – Ed Morton Mar 29 '22 at 14:44