3

次のようなファイルがあります。

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

(等。)

私はそれを次のようなものに変換したい:

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

これらのセクションヘッダーを取得して列に移動し、完全に説明的なテーブルを作成する方法がわかりません... awkを使用するのが最も簡単だと思いますが、誰かがコードのスニピットを提供したり、方法を説明したりできますこれはawk(または最も適切なコマンド)で実行できますか?

ありがとう!

4

2 に答える 2

5

以下に説明するデータを含む「test.txt」というファイルがあるとします。次のAWK行で取得できます。

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

Num Fieldsが1の場合、それはヘッダーなので、「header」変数に保存します。Num Fieldsが1より大きい場合、それは「データ行」であるため、「header」の最後の値と「dataline」全体を出力します。

それは私のために働いた:

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
于 2012-09-24T16:39:48.963 に答える
1

これはあなたのために働くかもしれません(GNU sed):

sed -i '/,/!{h;d};G;s/\(.*\)\n\(.*\)/\2, \1/' file
于 2012-09-25T13:07:03.287 に答える