bash - How to merge rows from the same column using unix tools -
i have text file looks following:
1000000 45 m line line line breaks breaks has blank multiple rows multiple rows - row below. how annoying! 1000001 50 f behaved. column has text spanning multiple rows i convert csv file looks like:
1000000, 45, m, line breaks multiple rows, line breaks multiple rows - how annoying! 1000001, 50, f, column has text spanning multiple rows, behaved. the text file output comes program written in 1984, , have no way modify output. want in csv format can convert excel painlessly possible. not sure start, , rather reinvent wheel, hoping point me in right direction. thanks!
== edit ==
i've modified text file have \n between rows - maybe helpful?
== edit 2 ==
i've modified text file have blank row.
using gnu awk
gawk ' begin { fieldwidths="11 6 5 22 22" } length($1) == 11 { if ($1 ~ /[^[:blank:]]/) { if (f1) print_line() f1=$1; f2=$2; f3=$3; f4=$4; f5=$5 } else { f4 = f4" "$4; f5 = f5" "$5 } } function rtrim(str) { sub(/[[:blank:]]+$/, "", str) return str } function print_line() { gsub(/[[:blank:]]{2,}/, " ", f4); gsub(/"/, "&&", f4) gsub(/[[:blank:]]{2,}/, " ", f5); gsub(/"/, "&&", f5) printf "%s,%s,%s,\"%s\",\"%s\"\n", rtrim(f1), rtrim(f2), rtrim(f3),f4,f5 } end {if (f1) print_line()} ' file 1000000,45,m,"this line breaks multiple rows ","this line breaks multiple rows - how annoying!" 1000001,50,f,"i column has text spanning multiple rows","i behaved. " i've quoted last 2 columns in case contain commas, , doubled potential inner double quotes.
Comments
Post a Comment