LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problems looping on .csv files (https://www.linuxquestions.org/questions/linux-newbie-8/problems-looping-on-csv-files-4175509110/)

eudald 06-25-2014 05:09 AM

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

kbnuts 06-25-2014 05:13 AM

you could try
Code:

echo $i a
with no quotes.

eudald 06-25-2014 05:15 AM

Same result kbnuts :(

kbnuts 06-25-2014 05:27 AM

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.

chrism01 06-25-2014 05:28 AM

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.

eudald 06-25-2014 05:30 AM

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!

kbnuts 06-25-2014 05:30 AM

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!


All times are GMT -5. The time now is 11:34 PM.