How to read a single column CSV file in bash?

hoojacks

I am relatively new to bash/programming in general.

I have a single column CSV that looks like this:

domain1.com
domain2.com
domain3.com
domain4.com

I want to run through each entry and do something with it. Here is my code:

foo(){
i=0
  while read -a line; 
    do
      echo ${line[i]}
      ((i++))
    done < myfile.csv 
}

And nothing happens. I have figured out that if I change the file I'm pointing at to:

done< <(grep '' myfile.csv)

it will work, but only spit out the very last line of the CSV, like this:

domain4.com 

Again, I am a beginner and teaching myself this stuff, so any explanations you want to give with your answers would be GREATLY appreciated!

EDIT So it appears that my new problem is removing the ^M character from my CSV file. Once I figure out how to do this, I will mark the answer here that works for me.

anubhava

Looks like you have 2 issues:

  1. Your lines are all ending with \r
  2. There is no new line or \r at the end of last line

To fix this issue use this script:

echo >> file.csv
while read -r line; do echo "$line"; done < <(tr '\r' '\n' < file.csv)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to read a single column CSV file in bash?

From Dev

Quicker way to read single column of CSV file

From Dev

Quicker way to read single column of CSV file

From Dev

How to read a single file in Heroku via bash

From Dev

R - Read a SPECIFIC, Single NUMERIC column in CSV file as a string

From Dev

How to read in csv file to array in bash script

From Dev

how to load a single-column csv file into a specific column in mysql

From Dev

How to split single column to multiple column in CSV file

From Dev

Read a file with a single line in bash

From Dev

How to read 2 different dataframes from single CSV file in R?

From Dev

Transpose a single column of a CSV file

From Dev

read the csv file column wise

From Dev

How to merge multiple csv files into an single csv file with single header also remove duplicates based on the certain column

From Dev

Read a single column of a CSV and store in an array

From Dev

How to read values in a particular column of a CSV file in Batch?

From Dev

How to read a CSV file column wise using hadoop?

From Dev

How to read CSV file specified column without using pandas?

From Dev

How to read values in a particular column of a CSV file in Batch?

From Dev

How to read a CSV file column wise using hadoop?

From Dev

How to read the second column in csv file using PHP?

From Dev

How to read from mysql and write column wise in csv file?

From Dev

How to read a column of time from a csv file in R for mathematical operations?

From Dev

How to read data from csv file if all the values are in the same column?

From Dev

How to extract the nth column of csv file with condition in Linux bash?

From Dev

BASH - How to extract data from a column in CSV file and put it in an array?

From Dev

Read file without delimiters (as a single column) in SAS

From Dev

How to modify a column of a csv in bash

From Dev

Pandas: read_csv (read multiple tables in a single file)

From Dev

Read in csv file with one column of strings in the middle

Related Related

  1. 1

    How to read a single column CSV file in bash?

  2. 2

    Quicker way to read single column of CSV file

  3. 3

    Quicker way to read single column of CSV file

  4. 4

    How to read a single file in Heroku via bash

  5. 5

    R - Read a SPECIFIC, Single NUMERIC column in CSV file as a string

  6. 6

    How to read in csv file to array in bash script

  7. 7

    how to load a single-column csv file into a specific column in mysql

  8. 8

    How to split single column to multiple column in CSV file

  9. 9

    Read a file with a single line in bash

  10. 10

    How to read 2 different dataframes from single CSV file in R?

  11. 11

    Transpose a single column of a CSV file

  12. 12

    read the csv file column wise

  13. 13

    How to merge multiple csv files into an single csv file with single header also remove duplicates based on the certain column

  14. 14

    Read a single column of a CSV and store in an array

  15. 15

    How to read values in a particular column of a CSV file in Batch?

  16. 16

    How to read a CSV file column wise using hadoop?

  17. 17

    How to read CSV file specified column without using pandas?

  18. 18

    How to read values in a particular column of a CSV file in Batch?

  19. 19

    How to read a CSV file column wise using hadoop?

  20. 20

    How to read the second column in csv file using PHP?

  21. 21

    How to read from mysql and write column wise in csv file?

  22. 22

    How to read a column of time from a csv file in R for mathematical operations?

  23. 23

    How to read data from csv file if all the values are in the same column?

  24. 24

    How to extract the nth column of csv file with condition in Linux bash?

  25. 25

    BASH - How to extract data from a column in CSV file and put it in an array?

  26. 26

    Read file without delimiters (as a single column) in SAS

  27. 27

    How to modify a column of a csv in bash

  28. 28

    Pandas: read_csv (read multiple tables in a single file)

  29. 29

    Read in csv file with one column of strings in the middle

HotTag

Archive