LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-07-2019, 09:30 AM   #1
mhashemi
LQ Newbie
 
Registered: Mar 2019
Location: New Jersey, 07726
Posts: 5

Rep: Reputation: Disabled
Lightbulb How to read a CSV file in a shell script


How to read a CSV file in a shell script.

Mort Hashemi
 
Old 03-07-2019, 09:47 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Welcome to LinuxQuestions.

You should be able to find examples by searching the web. Using IFS=, you can use a loop and read each field into a separate variable or an array. Here is an old example.

https://www.cyberciti.biz/faq/unix-l...rated-cvsfile/

There isn't a standard that defines a field so depending on your data format reading a CSV file using bash may not be as simple as the above example.
 
Old 03-07-2019, 10:34 AM   #3
mhashemi
LQ Newbie
 
Registered: Mar 2019
Location: New Jersey, 07726
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you michaelk, I tried the script that you suggested, but IFS loop does not return any values. Any idea why?

for file_name in $search_dir/*.idx
do
oldifs=$ifs
while ifs=, read a1 a2 a3 a4 a5;
do
echo $a1 $a2 $a3 $a5
done < file_name

ifs=$oldifs
done
 
Old 03-07-2019, 10:57 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,703

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Code:
for file_name in $search_dir/*.idx
do
oldifs=$ifs
while ifs=, read a1 a2 a3 a4 a5;
do
echo $a1 $a2 $a3 $a5
done < file_name  <--- You missed the $ in the variable name.

ifs=$oldifs
done
Some syntax errors but it depends on what shell you are using and assume bash. You can check your script using shellcheck.net.
Make sure you define search_dir.

Code:
#!/bin/bash

for file_name in $search_dir/*.idx
do

  while ifs=, read a1 a2 a3 a4 a5;
  do
    echo $a1 $a2 $a3 $a4 $a5
  done < "$file_name" 

done
 
Old 03-07-2019, 11:32 AM   #5
mhashemi
LQ Newbie
 
Registered: Mar 2019
Location: New Jersey, 07726
Posts: 5

Original Poster
Rep: Reputation: Disabled
I tried it with $ and without. Also tried enclosed in ("). None of them worked.
 
Old 03-07-2019, 11:49 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
ifs should be UPPERCASE
 
Old 03-07-2019, 12:35 PM   #7
mhashemi
LQ Newbie
 
Registered: Mar 2019
Location: New Jersey, 07726
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you pan64. I change ifs to IFS. still not going through loop. Looks like that the "while IFS=,...." not reading $file_name. $file_name has the path and filename:
echo $file_name give me this: "/home/admin/sps/CBD27.idx" the file CBD27.idx is a CSV file.
 
Old 03-07-2019, 12:47 PM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
I would say insert set -xv at the beginning and you will see what's happening Probably you will find the reason (this will not solve anything, just display a lot of logs)
 
1 members found this post helpful.
Old 03-07-2019, 02:08 PM   #9
mhashemi
LQ Newbie
 
Registered: Mar 2019
Location: New Jersey, 07726
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thank you pan64. It is working now.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to print lines in csv file if 1 csv column field = "text". There are 10 column (;) in csv file nexuslinux Linux - Newbie 9 04-22-2016 11:35 PM
[SOLVED] A challenging script - Replace field of CSV file based on another CSV file arbex5 Programming 11 06-12-2013 06:56 AM
[SOLVED] How to script csv editing? Remove rows from csv file that do not contain certain text ingram87 Linux - Software 9 08-03-2012 12:45 PM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
Shell script to read from csv file hendemeg Programming 1 05-11-2004 08:23 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration