0

I want to parsing the following csv file:

$ cat file.csv 
11:16:16:31:67:01,123456789a123456789b123456789c123456789d123456789e
11:16:16:31:67:02,123456789a123456789b123456789c123456789d123456789e
11:16:16:31:66:FF,123456789a123456789b123456789c123456789d123456789e
11:16:16:31:67:00,123456789a123456789b123456789c123456789d123456789e

Here is my code:

$ cat read.sh 
#! /bin/sh
OLDIFS=$IFS
IFS=','
while read f1 f2; do
    echo "$f1, $f2"
done < file.csv
IFS=$OLDIFS

It seems work wells:

$ ./read.sh 
11:16:16:31:67:01, 123456789a123456789b123456789c123456789d123456789e
11:16:16:31:67:02, 123456789a123456789b123456789c123456789d123456789e
11:16:16:31:66:FF, 123456789a123456789b123456789c123456789d123456789e
11:16:16:31:67:00, 123456789a123456789b123456789c123456789d123456789e

But if I change the order of output:

from

while read f1 f2; do
    echo "$f1, $f2" # <= change order
done < file.csv

to

while read f1 f2; do
    echo "$f2, $f1"
done < file.csv

Then the output is not my expected format. I don't know why? Can someone give me a tips? Thanks.

$ ./read.sh 
, 11:16:16:31:67:01b123456789c123456789d123456789e
, 11:16:16:31:67:02b123456789c123456789d123456789e
, 11:16:16:31:66:FFb123456789c123456789d123456789e
, 11:16:16:31:67:00b123456789c123456789d123456789e
1
  • 1
    Use awk for better handling of csv files if not considering a csv parser in perl, python, php. You input may have DOS line endings i.e. \r Commented Dec 6, 2021 at 7:53

1 Answer 1

2

After remove CR, it works well:

here is my test:

$ file file.csv 
file.csv: ASCII text, with CRLF line terminators

$ dos2unix file.csv 
dos2unix: converting file file.csv to Unix format...

$ file file.csv 
file.csv: ASCII text

and it works well:

$ ./read.sh 
123456789a123456789b123456789c123456789d123456789e, 11:16:16:31:67:01
123456789a123456789b123456789c123456789d123456789e, 11:16:16:31:67:02
123456789a123456789b123456789c123456789d123456789e, 11:16:16:31:66:FF
123456789a123456789b123456789c123456789d123456789e, 11:16:16:31:67:00
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.