shell - Bash Parse CSV - Formating Datasets -
i have csv following format
dataset1, … … dataset2, .. .. dataset3,
all datasets seperated blank lines. bash script change formatting of file to
dataset1 dataset2 dataset3 ... … … … … … … … …
here's script, or guidance appreciated
#!/bin/bash input="/path/to/csv/file/file.cvs" while ifs=',' read -r f1 f2 f3; if [ -z "$f1 $f2 $f3" ]; awk 'begin{getline to_add < "$f1 $f2 $f3"}{print $0,to_add}' f fi echo "$f1 $f2 $f3" done < "$input"
the below pure shell (no embedded awk/perl) work. has limitations. works same number of records in each set. handle differnt numbers need mainting count of records in each set , embed emty records ,,,,,,,
required.
set -u row=0 set=1 maxrow=0 while read line if [ -z "$line" ] # new data set if [ $row -gt $maxrow ] maxrow=$row fi row=0 set=$(($set+1)) elif [ $set -eq 1 ] data[$row]="$line" row=$(($row+1)) else data[$row]="${data[$row]},$line" row=$(($row+1)) fi done if [ $row -gt $maxrow ] maxrow=$row fi row=0 while [ $row -lt $maxrow ] echo "${data[$row]}" row=$(($row+1)) done
Comments
Post a Comment