我有一个如下所示的文件:
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 (或任何最合适的命令)来实现这一点吗?
谢谢!
发布于 2012-09-25 00:39:49
给定一个名为"test.txt“的文件,其中包含您在下面描述的数据。您可以使用下面的AWK代码行获取它:
awk -F"," 'NF == 1 {header = $0;} NF > 1 {print header", "$0;}' test.txt如果Num Fields为1,则它是一个标题,因此我将其保存在" header“变量中。如果Num Fields大于1,则它是"data line",所以我打印"header“的最后一个值和整个"data line”。
这对我来说很有效:
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-25 21:07:03
这可能适用于您(GNU sed):
sed -i '/,/!{h;d};G;s/\(.*\)\n\(.*\)/\2, \1/' filehttps://stackoverflow.com/questions/12569177
复制相似问题