我想从输出文件的特定位置获取变量,输入文件格式如下:
log1.txt format:
[v] Output Data
<Value>DIMM_A,4096,1600,Hynix,HMT351S6CFR8C-PB,0942E041,1206,01,Hynix,,</Value>
or log2.txt format:
[v] Output Data
<Value>DIMM_B,4096,1600,Hynix,HMT351S6CFR8C-PB,017E90AE,1205,01,Hynix,,</Value>
<Value>DIMM_A,4096,1600,Hynix,HMT351S6CFR8C-PB,012E908D,1205,01,Hynix,,</Value>
我们希望得到如下格式的输出OUT.TXT文件:
if log1.txt format and then output file format:
SET DIMM1=DIMM_A,4096,1600,Hynix,HMT351S6CFR8C-PB,0942E041,1206,01,Hynix,,
if log2.txt format and then output file format:
SET DIMM2=DIMM_B,4096,1600,Hynix,HMT351S6CFR8C-PB,017E90AE,1205,01,Hynix,,
SET DIMM1=DIMM_A,4096,1600,Hynix,HMT351S6CFR8C-PB,012E908D,1205,01,Hynix,,
你能帮助我吗?谢谢!
答案1
主要问题是我们不知道Value
有多少行。因此解决方案有点复杂:
sed -n '/^<Value/{s/^<[^>]*>/=/;s/<[^>]*>$//;p}' log1.txt | sort | cat -n |\
sed 's/^\s\+\([0-9]*\)\s\+=/SET DIMM\1=/'
其中第一个sed
greps 包含 Value 的行并删除<>
标签。之后sort
将对行进行排序,cat -n
枚举它们,最后sed
将行号移入DIMM-num-
。
这样awk
就非常接近sed
解决方案了:
awk -F'>' '/^<Value/{gsub ("<.*", "", $2);print $2}' log1.txt | sort |\
awk '{print "SET DIMM"NR"="$0}'