0

I have a csv file that looks like this:

arruba,jamaica, bermuda, bahama, keylargo, montigo, kokomo
80,70,90,85,86,89,83
77,75,88,87,83,85,77
76,77,83,86,84,86,84

I want to have a shell script set up so that I can extract out the data so that I can categorize data by columns.

I know that the line of code:

IFS="," read -ra arr <"vis.degrib"
for ((i=0 ; i<${#arr[@]} ; i++));do
     ifname=`printf "%s\n" "${arr[i]}"`
     echo "$ifname"
done

will print out the individual column components for the first row. How do I also do this again for subsequent rows?

Thank you for your time.

2
  • What's your expected output? Commented Aug 19, 2015 at 17:45
  • I don't think your script works as you intended. Commented Aug 19, 2015 at 17:48

1 Answer 1

1

I'm extrapolating from the OP

 awk -F, 'NR==1{for(i=1;i<=NF;i++) {gsub(/^ +/,"",$i);print $i}}' vis.degrib

will print

arruba
jamaica
bermuda
bahama
keylargo
montigo
kokomo

note that there is trimming of the space from the beginning of each field. If you remove the condition NR==1, the same will be done for all rows. Was this your request? Please comment...

Perhaps you want to convert columnar format to row based format (transpose)? There are many ways, this awk script will do

 awk -F, -v OFS=, '{sep=(NR==1)?"":OFS} {for(i=1;i<=NF;i++) a[i]=a[i] sep $i} END{for(i=1;i<=NF;i++) print a[i]}' vis.degrib

will print

arruba,80,77,76
jamaica,70,75,77
 bermuda,90,88,83
 bahama,85,87,86
 keylargo,86,83,84
 montigo,89,85,86
 kokomo,83,77,84

you can again trim the space from the beginning of the labels as shown above.

Another approach without awk.

 tr ',' '\n' <vis.degrib | pr -4ts,

will generate the same

 arruba,80,77,76
 jamaica,70,75,77
  bermuda,90,88,83
  bahama,85,87,86
  keylargo,86,83,84
  montigo,89,85,86
  kokomo,83,77,84

4 is the number of rows in the original file.

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.