在 linux 中的特定匹配后将列值转换为行
convert column values to row after particular match in linux
我有一个文件,其中所有值都在重复模式下的 1 列中(1 组值出现在第 10 行之后)。现在我想以重复模式将这组值从列到行(设置明智)。显示一些例子如下
A = 1
B = 2
C = 3
-----
A = 4
B = 5
C = 6
这里我想要如下输出
1,2,3
4,5,6
谁能帮我解决这个问题?
我会说
awk '/^-----/ { print line; line = ""; sep = ""; next } { line = line sep ; sep = "," } END { print line }' filename
其工作原理如下:
/^-----/ { # 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 # 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.
}
带有分隔符的同一系列的行(如样本)
sed '/^---/ b treat
s/[[:blank:]]\{1,\}//g;H;$!d
:treat
s/.*//;x
s/\n[^=]*=/,/g;s/,//
' YourFile
假设:
- 只有数字作为值
- 总有一个正确的系列长度(不提供空值是 pe:
B=
缺失)
我有一个文件,其中所有值都在重复模式下的 1 列中(1 组值出现在第 10 行之后)。现在我想以重复模式将这组值从列到行(设置明智)。显示一些例子如下
A = 1
B = 2
C = 3
-----
A = 4
B = 5
C = 6
这里我想要如下输出
1,2,3
4,5,6
谁能帮我解决这个问题?
我会说
awk '/^-----/ { print line; line = ""; sep = ""; next } { line = line sep ; sep = "," } END { print line }' filename
其工作原理如下:
/^-----/ { # 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 # 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.
}
带有分隔符的同一系列的行(如样本)
sed '/^---/ b treat
s/[[:blank:]]\{1,\}//g;H;$!d
:treat
s/.*//;x
s/\n[^=]*=/,/g;s/,//
' YourFile
假设:
- 只有数字作为值
- 总有一个正确的系列长度(不提供空值是 pe:
B=
缺失)