linux中特定匹配后将列值转换为行
我有一个文件,其中的值都在重复模式下的1列中(1组值位于10行之后)。现在我想把这组值从列到行(按集合)放在重复模式中。下面显示一些示例linux中特定匹配后将列值转换为行,linux,awk,sed,scripting,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 有谁能帮我解决这个问题吗?我想说 awk '/^-----/ { print line; line = ""; sep = ""; next } { line = line sep $3; sep = "," } END {
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 $3; 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 $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.
}
/^-----/{{如果当前行是分隔符
打印行#打印我们刚刚构建的内容(见下文)
行=”“#重置状态变量
sep=“”
接下来,别做别的
}
{#否则:
行=行sep$3#将当前行的第三个字段附加到输出
sep=“,”#带有分隔符的同一系列行(与样本中相同)
假设:
- 只有数字作为值
- 始终存在正确的序列长度(不包含空值的提要为pe:
B=
缺失)
有些值是以空格分隔的,因此$4$5的值不会包含在最终输出中。你知道如何包含这些值吗?你可以使用sub(/^[^=]*=*=*/,“”);line=line sep$0;
而不是line=line sep$3;
,例如,这将删除第一个=
(以及其后的空格),然后将行的其余部分用作标记。如果您选择该路线,可能还需要删除行末尾的空格(例如,sub(/*$/,“”);
)。
sed '/^---/ b treat
s/[[:blank:]]\{1,\}//g;H;$!d
:treat
s/.*//;x
s/\n[^=]*=/,/g;s/,//
' YourFile