awk:使用包含标题的部分修改csv

use*_*475 3 unix awk sed tr

我有一个看起来像这样的文件:

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
Run Code Online (Sandbox Code Playgroud)

(等等.)

我希望它转换成这样的东西:

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
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用这些节标题并将它们移动为列,以便我有一个完全描述性的表...我相信用awk最简单,但有人可以提供代码片段,或解释如何这可以用awk(或任何最合适的命令)来完成?

谢谢!

aru*_*aku 5

给定一个名为"test.txt"的文件,其中包含您在下面描述的数据.您可以使用以下AWK行获取它:

awk -F"," 'NF == 1 {header = $0;} NF > 1 {print header", "$0;}' test.txt
Run Code Online (Sandbox Code Playgroud)

如果Num Fields为1则为标题,因此我将其保存在"header"变量中.如果Num Fields大于1,则它是"数据行",因此我打印"header"的最后一个值和整个"数据行".

它对我有用:

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
Run Code Online (Sandbox Code Playgroud)