1

in a folder, I have 2 files. Valuation.csv and another file myScript.sh

My CSV file has 10 lines and 5 columns. I have tried to read it multiple times but it never worked. This is what I have tried as code inside my myScript.sh:

First try:

#!/bin/bash

    while read -r line do
        field1=$(echo $line | awk -F'|' '{printf "%s", $1}' | tr -d '"')
        field2=$(echo $line | awk -F'|' '{printf "%s", $2}' | tr -d '"')

        echo $field1 $field2 done < $Valuation.csv

Result: /myScript.sh: line 10: .csv: No such file or directory

Second try:

cat Valuation.csv|while read line   do
read -d, col1 col2 < <(echo $line)
echo "I got:$col1|$col2"   done

Result: nothing

I am running the file like this:

./myScript.sh

thank you

Here the csv file opened with excel

4
  • why not use general purpose scriping languages such as python/perl? Commented Mar 13, 2019 at 0:01
  • Hi @James can you provide the content of one file? Commented Mar 13, 2019 at 0:07
  • 1
    Save the excel worksheet as a .csv and post the data as text, not a picture of the spreadheet. Bash can't read a picture of the spreadsheet. Commented Mar 13, 2019 at 1:35
  • @James : The variable Valuation is empty/undefined. Aside from this, I don't think it is a good idea to process CSV in this way, as it works only with fairly simple CSV files. For instance, the awk part does not work if one of the fields contain a (quoted) | as value. Commented Mar 13, 2019 at 8:18

2 Answers 2

1

here is my file data.csv

Site,Post,Subject,User,Status
stackover flow,bash,read csv,James,ok
git,linux,core,Novy,ko

in my bash script i have something like:

#!/bin/bash
INPUT=./data.csv
OLDIFS=$IFS
IFS=,
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read site post subject usr status
do
    echo "Site : $site"
    echo "Post : $post"
    echo "Subject : $subject"
    echo "User : $usr"
    echo "Status : $status"
done < $INPUT
IFS=$OLDIFS

Output

Site : stackover flow Post : bash Subject : read csv User : James Status : ok Site : git Post : linux Subject : core User : Novy Status : ko

You can give a look here

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, can you show me a picture of your file? are "Site,Post,Subject,User,Status" columns?
a csv file is a text file with a comma or semicolon separator. The file content is at the begining of the post. Copy it and create your csv file locally. I hope it is clear the important thing is the "while read site post subject usr status" after that you can use the variable with the "$" plus variable name
1

James, avoid calling utilities within a loop in bash. That is horribly inefficient. Avoid using a fixed number of fields -- what if another is added? You can read .csv data in bash fairly easily by simply using read -a array... and setting the internal field separator to word-split on a comma (e.g. IFS=$',\n').

For example, writing a script that takes the .csv filename to read as the first argument, you could do:

#!/bin/bash

declare -i row=0

while IFS=$',\n' read -r -a array; do
    printf "row %d - read %d fields.\n" $((row++)) ${#array[@]}
    for i in "${array[@]}"; do
        printf " %-12s" "$i"
    done
    echo ""
done < "$1"

(note: while you would in-practice want to handle the entire matter in awk, since this appears to be a bash exercise, for short .csv files, bash is more than capable)

Example Data

(I wasn't going to retype your spreadsheet, so here are the first two rows)

$ cat company.csv
Facebook,35587,55800000000,1567988,491000000000,8.80x
Uber,16000,11300000000,706250,120000000000,10.6x

Running the script and simply outputting the row count and fields-per-row followed by the field values themselves, you would get:

$ bash readcsv.sh company.csv
row 0 - read 6 fields.
 Facebook     35587        55800000000  1567988      491000000000 8.80x
row 1 - read 6 fields.
 Uber         16000        11300000000  706250       120000000000 10.6x

Look things over and let me know if you have questions.

2 Comments

Thank you David for your answer. I implemented the first script and it is working but the format is totally off. thanks, However, I do not really get what is the purpose of the example data you wrote later on. Do I need to have a line of code with "$ cat Valuation.csv? Should I implement it within the myScrip.sh file?
You can change the format of the printf statement -- I just used a left-justified 12 character field-width to ensure even separation between the fields. No need to cat validation. The purpose of the data shown is because you posted a PICTURE of your data rather than your actual data. Most people will ignore your question for that reason -- they are not going to re-type your data. I re-typed two lines of it in .csv format. You simply provide the filename.csv as the first argument to the script. (you can add a read to consume the header row if needed)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.