-2

I have a CSV file with several rows and columns, some rows have 4 columns and some have 5 columns. I want to add to those with 4 columns, one more column so all the rows have 5 columns. The info that I must add must be in the 3rd row, or at the end. The CSV file looks like this:

name;ip;supplier;os;manufact

How can this be done in bash?

wizulus
  • 5,653
  • 2
  • 23
  • 40
sergy
  • 1

1 Answers1

0

You can use read to split the CSV, then output the desired number of columns.

cat test.csv | while read line; do
  IFS=\; read -ra COL <<< "$line"
  echo "${COL[0]};${COL[1]};${COL[2]};${COL[3]};${COL[4]};"
done

For example, with test.csv containing:

1;2;3;4
1;2;3;4;5
1;2;3;4;
1;2;3;4;5;

The above script outputs:

1;2;3;4;;
1;2;3;4;5;
1;2;3;4;;
1;2;3;4;5;
wizulus
  • 5,653
  • 2
  • 23
  • 40