Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Awk to create a csv line

Tags:

sed

awk

I am new to awk and can't quite figure out the best way to do this. I have thousands of xml files which I have already removed duplicates and divided fields into a single column in a single file using sed and awk.

Now I want to assemble the list into a csv file containing multiple fields on one line. After a fixed number of fields I want to start a new line.

Example

1234
2345

345678
4.23456E3
54321
654321
789

87654.100
9876

10.0
1234
2345

345678
4.23456E3
54321
654321
789

87654.100
9876

11.0

Output

1234, 2345, , 345678, 4.23456E3, 54321, 654321, 789, , 87654.100, 9876, , 10.0
1234, 2345, , 345678, 4.23456E3, 54321, 654321, 789, , 87654.100, 9876, , 11.0

Thanks

like image 251
Road King Avatar asked Sep 15 '26 01:09

Road King


1 Answers

Is using xargs allowed?

cat input | xargs -L13 -d'\n' | sed -e 's/ /, /g'

I get this output here:

1234, 2345, , 345678, 4.23456E3, 54321, 654321, 789, , 87654.100, 9876, , 10.0
1234, 2345, , 345678, 4.23456E3, 54321, 654321, 789, , 87654.100, 9876, , 11.0

It's sort of hackey, though, if you started out with XML you should consider using XSLT.

like image 57
cha0site Avatar answered Sep 17 '26 22:09

cha0site



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!