Home > front end >  BASH script to create SQL statement ignore last column
BASH script to create SQL statement ignore last column

Time:12-29

I am trying to create a bash script that will generate an SQL CREATE TABLE statement from a CSV file.

#!/bin/bash

# Check if the user provided a CSV file
if [ $# -eq 0 ]
then
    echo "No CSV file provided."
    exit 1
fi

# Check if the CSV file exists
if [ ! -f $1 ]
then
    echo "CSV file does not exist."
    exit 1
fi

# Get the table name from the CSV file name
table_name=$(basename $1 .csv)

# Extract the header row from the CSV file
header=$(head -n 1 $1)

# Split the header row into column names
IFS=',' read -r -a columns <<< "$header"

# Generate the PostgreSQL `CREATE TABLE` statement
echo "CREATE TABLE $table_name ("
for column in "${columns[@]}"
do
    echo "  $column TEXT,"
done
echo ");"

If I have a CSV file with three columns(aa,bb,cc), the generated statement does not have the last column for some reason.

Any idea what could be wrong?

If I do:

for a in "${array[@]}"
do
    echo "$a"
done

I am getting:

aaa
bbb
ccc

But when add something into the string:

for a in "${array[@]}"
do
    echo "$a SOMETHING"
done

I get:

aaa SOMETHING
bbb SOMETHING
 SOMETHING

Thanks.

CodePudding user response:

Your csv file has a '\r`
Try the next block for reproducing the problem.

printf -v header "%s,%s,%s\r\n" "aaa" "bbb" "ccc"
IFS=',' read -r -a columns <<< "$header"
echo "Show array"
for a in "${columns[@]}"; do echo "$a"; done
echo "Now with something extra"
for a in "${columns[@]}"; do echo "$a SOMETHING"; done

You should remove the '\r', what can be done with

IFS=',' read -r -a columns < <(tr -d '\r' <<< "${header}")
  • Related