Duplicate CSV column in Bash

I have a problem where a client needs to duplicate a column in a CSV file. The values ​​will always be identical, and, unfortunately, our API does not allow specifying duplicate columns in JSON.

For example, I have the following column structure and values:

Name, Surname, City, Age, Job
John, Doe, Johannesburg, 28, Technical Support

Now I need to duplicate the City so that the output is:

Name, Surname, City, City Again, Age, Job
John, Doe, Johannesburg, Johannesburg, 28, Technical Support

The column must be placed after it will be duplicated. The value also depends on this first column.

+4
source share
4 answers

Assuming it Citywill always be in column 3, cut and paste can be utlized. For instance:

csv=path/to/somefile.csv
echo "$(paste -d',' <(cut -d',' -f1-3 $csv) <(cut -d',' -f3- $csv))" > "$csv"

Notes:

  • path/to/quux.csv, csv, .csv.
  • , City, .
  • <(...) , . .
0

awk :

awk 'BEGIN{FS=OFS=", "} {$3 = $3 OFS $3} 1' file.csv

Name, Surname, City, City, Age, Job
John, Doe, Johannesburg, Johannesburg, 28, Technical Support

, , , , cut , `paste.

@codeforester , cut ; .

+3

A , bash :

function array_insert {
    # options: arrayname index [value]
    if ! declare -p "$1" 2>/dev/null | grep -q '^declare -a'; then
        printf '%s: not an array: %s\n' "$0" "$1" >&2
        return 1
    fi
    local -n source="$1"
    local -a indices=( "${!source[@]}" )
    for ((i=${#indices[@]}-1; i>=$2; i--)) ; do
        source[$((i+1))]="${source[$i]}"
    done
    if [ -n "$3" ]; then
        source[$2]="$3"
    fi
}

:

while read line; do
  IFS=, declare a=( $line )     # assign the line fields to an array,
  array_insert a 3 "${a[2]}"    # insert the column in this line,
  o=$(printf '%s,' "${a[@]}")   # assemble your output,
  printf '%s\n' "${o%,}"        # remove the trailing comma.
done < input.txt

:

Name, Surname, City, City, Age, Job
John, Doe, Johannesburg, Johannesburg, 28, Technical Support

, bash 0, ${a[2]} .

0

script,

script

echo "Enter  CSV File  name "
read fileName

echo "Enter Column name to be duplicated "
read columnName


columnNumber=`head -1 $fileName | awk -v RS="," "/$columnName/{print NR;}"`     #Identify Column number using column name
totalNumberOfColumn=`head -1 $fileName | awk -F',' '{print NF}'`                #identify total number of column

str=""                                                                          #Create empty variab str to print column number in awk
for ((i=1;i<=$totalNumberOfColumn;i++));
do
str="$str \$$i\",\""
        if [ $i == $columnNumber ]
        then
        str="$str \$$i\",\""
        fi
done


awk -F',' "{print ${str}}" $fileName  | sed 's/,$//g'                           #Print all column inculding duplicate column 

- , data.csv

:cat data.csv
Name, Surname, City, Age, Job
John, Doe, Johannesburg, 28, Technical Support

1

:bash script.sh
Enter  CSV File  name
data.csv
Enter Column name to be duplicated
City
Name, Surname, City, City, Age, Job
John, Doe, Johannesburg, Johannesburg, 28, Technical Support

2:

:bash script.sh
Enter  CSV File  name
data.csv
Enter Column name to be duplicated
Name
Name,Name, Surname, City, Age, Job
John,John, Doe, Johannesburg, 28, Technical Support
0

Source: https://habr.com/ru/post/1677901/


All Articles