在linux中特定匹配后将列值转换为行

时间:2015-09-22 10:42:27

标签: linux awk sed scripting

我有一个文件,其中值在重复模式下都在1列中(1组值在10行之后)。现在我想在重复模式中将这组值从列到行(设置为明智)。显示如下的一些例子

A = 1  
B = 2
C = 3
-----
A = 4
B = 5
C = 6

这里我想要输出如下

1,2,3
4,5,6

有谁可以帮我解决这个问题?

2 个答案:

答案 0 :(得分:2)

我说

/^-----/ {               # If the current line is a delimiter
  print line             # print the stuff we just constructed (see below)
  line = ""              # reset state variables
  sep = ""
  next                   # do nothing else
}
{                        # otherwise:
  line = line sep $3     # append 3rd field of the current line to the output
  sep = ","              # <-- the first time around, sep is "", so there's no
                         #     comma before the first field in the output.
}
END {                    # When the input ends:
  print line             # print the last record.
}

其工作原理如下:

var user = {
  "username":"testuser update",
  "email":"user001@mail.co.th",
  "name":"user001",
  "ownAccount":"[
    {"id":2,"name":"Demo Account2"},
    {"id":1,"name":"Demo Account"}
  ]"
}

答案 1 :(得分:0)

相同系列的线与分隔符(如样品中)

sed '/^---/ b treat
     s/[[:blank:]]\{1,\}//g;H;$!d
:treat
     s/.*//;x
     s/\n[^=]*=/,/g;s/,//
     ' YourFile

假设:

  • 只有数字作为值
  • 总是有一个正确的系列长度(没有空值的饲料是pe:B=缺失)