unix - Awk:使用包含标题的部分修改 csv

标签 unix sed awk 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

(等)

我想把它转换成这样:

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(或任何最合适的命令)完成此操作?

谢谢!

最佳答案

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

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

如果 Num Fields 为 1,则它是标题,所以我将其保存在“标题”变量中。 如果 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

关于unix - Awk:使用包含标题的部分修改 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12569177/

相关文章:

libc.so.1 中的核心

linux - 用于访问网站的 Unix 脚本

php - Symfony3 : Warning:copy([. ..]Entity/User.php~): 权限被拒绝

linux - 查找不包含搜索字符串的文件

linux - awk,对于巨大的值没有正确理解

cocoa - 如何从 FSSpec 获取 NSString(Unix 风格路径)

macos - 使用 sed 查找并除以文件中的所有数字

regex - 选择两个模式之间的第一个匹配项。如果使用 sed/awk/grep 找到第三个模式,则重新开始搜索

linux - 如何将 sed 与模式文件和 sed 一起使用

awk - 计算任意列数的百分比