LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-25-2014, 05:09 AM   #1
eudald
LQ Newbie
 
Registered: Apr 2014
Posts: 10

Rep: Reputation: 1
Problems looping on .csv files


Hi guys,
I'm having problems trying to loop on .csv files, and this problem is new for me, checked elsewhere with no success.

I've a oneline script as simple as it could be:
Code:
for i in $(cat filename.csv); do echo "$i a"; sleep 1; done
filename.csv 2 first lines are:
email
_titi@email.com

The result, rather than the expected:
Code:
email a
_titi@email.com a
is:
Code:
aail
aiti@email.com
Looks like it's replacing rather than adding, and I'm very confused about...

Any clues?
Thanks!
Eudald
 
Old 06-25-2014, 05:13 AM   #2
kbnuts
Member
 
Registered: Apr 2014
Posts: 45

Rep: Reputation: Disabled
you could try
Code:
echo $i a
with no quotes.
 
Old 06-25-2014, 05:15 AM   #3
eudald
LQ Newbie
 
Registered: Apr 2014
Posts: 10

Original Poster
Rep: Reputation: 1
Same result kbnuts
 
Old 06-25-2014, 05:27 AM   #4
kbnuts
Member
 
Registered: Apr 2014
Posts: 45

Rep: Reputation: Disabled
so is the csv actually comma separated or just a list like you put there?
It seems to ignore the underscore and the em in email too from your output.

Let me check.
 
Old 06-25-2014, 05:28 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Certainly looks odd: could be a locale problem.
Was this file created on MSWin instead of Linux?

Try running the 'od' cmd http://linux.die.net/man/1/od against the file to check what's really there.
 
Old 06-25-2014, 05:30 AM   #6
eudald
LQ Newbie
 
Registered: Apr 2014
Posts: 10

Original Poster
Rep: Reputation: 1
I think I got it, I just filed the .csv and it's a CRLF line terminated.
I've seen related posts about it here, i.e.: http://www.linuxquestions.org/questi...4/#post3628548

Just to post the solution:
Code:
tr -d '\015' < in.csv > out.csv
Thank you guys!

Last edited by eudald; 06-25-2014 at 05:35 AM.
 
Old 06-25-2014, 05:30 AM   #7
kbnuts
Member
 
Registered: Apr 2014
Posts: 45

Rep: Reputation: Disabled
I just did it on mine with the contents you listed and got the correct output using your for.
Code:
[root@xcxcxcx ~]# for i in $(cat test.csv); do echo "$i a"; sleep 1; done
email a
_titi@email.com a
It might be some form of corruption in your file.

---------- Post added 06-25-14 at 05:30 AM ----------

Ah! Nice one!
 
  


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
looping through files to search for given substrings kabester LinuxQuestions.org Member Success Stories 1 08-02-2012 03:20 PM
Looping in Many Files with Shell Programming ali2011 Programming 5 07-27-2012 01:30 PM
[SOLVED] Looping through csv file to calculate avg/min/max HuJo Linux - Newbie 10 09-02-2010 09:25 AM
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
looping trough files BackwardsDown Programming 4 02-09-2007 10:39 PM

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

All times are GMT -5. The time now is 10:17 AM.

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