siddharth
siddharth

Reputation: 11

Parsing a CSV file in BASH

I have a csv as below I needed that formatted .

current format

a,b,c,d,e
1,2,3,4,5

Desired Output

a,
b,
c,
d,
e

1,
2,
3,
4,
5

Upvotes: 1

Views: 104

Answers (3)

Rachid K.
Rachid K.

Reputation: 5201

Try a script like this:

#!/bin/bash

file=$1

cat ${file} | while read line
do 
  echo $line | sed 's/,/,\n/g'
  echo
done

Or if you don't need a blank line after each translated lines:

cat filename.csv | sed 's/,/,\n/g'

Upvotes: 1

glenn jackman
glenn jackman

Reputation: 246744

You tagged and , so a bash answer:

while IFS= read -r line; do
    printf "%s\n\n" "${line//,/$',\n'}"
done < file.csv

Upvotes: 0

stackoverflower
stackoverflower

Reputation: 122

You can try with:

sed "1G;s/,/,\n/g" file.csv

Append a new line then replace "," with ",newline"

Upvotes: 0

Related Questions