Pregunta

I have a file that looks like this:

Downtown
3/1/2013, 6A, 1 convertible to 2 bedroom apartment, tile bath, air conditioning, $3800
4/1/2013, 4C, One bedroom living room eat-in-kitchen, $3850
East Village
5/17/2013, 9M, STUDIO K'ETTE air conditioner TILE BATH, $2300
6/1/2013, 6H, Studio with tile bath and kitchenette with stone counters tops, $2600
SoHo
6/1/2013, 10B ,STUDIO KETTE air conditioner TILE BATH, $2400

(etc.)

I want it converted to something like this:

Downtown, 3/1/2013, 6A, 1 convertible to 2 bedroom apartment, tile bath, air conditioning, $3800
Downtown, 4/1/2013, 4C, One bedroom living room eat-in-kitchen, $3850
East Village, 5/17/2013, 9M, STUDIO K'ETTE air conditioner TILE BATH, $2300
East Village, 6/1/2013, 6H, Studio with tile bath and kitchenette with stone counters tops, $2600
SoHo, 6/1/2013, 10B ,STUDIO KETTE air conditioner TILE BATH, $2400

I have no idea how to take these section headers and move them to be columns so that I have a fully descriptive table ... I believe it would be easiest to do with awk, but can someone provide a snipit of code, or explain how this can be done with awk (or whatever command is most appropriate)?

Thanks!

¿Fue útil?

Solución

Given a file called "test.txt" with the data that you described below. You can get it with the following AWK line:

awk -F"," 'NF == 1 {header = $0;} NF > 1 {print header", "$0;}' test.txt

If Num Fields is 1 the it is a header, so I save it in "header" variable. If Num Fields is greater than 1, then it is a "data line", so I print the last value of "header" and the whole "data line".

It worked for me:

Downtown, 3/1/2013, 6A, 1 convertible to 2 bedroom apartment, tile bath, air conditioning, $3800
Downtown, 4/1/2013, 4C, One bedroom living room eat-in-kitchen, $3850
East Village, 5/17/2013, 9M, STUDIO K'ETTE air conditioner TILE BATH, $2300
East Village, 6/1/2013, 6H, Studio with tile bath and kitchenette with stone counters tops, $2600
SoHo, 6/1/2013, 10B ,STUDIO KETTE air conditioner TILE BATH, $2400

Otros consejos

This might work for you (GNU sed):

sed -i '/,/!{h;d};G;s/\(.*\)\n\(.*\)/\2, \1/' file
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top