how to get access to environment variables from output file

0

I want to get variables from a output file specific location,and input file format as below:

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>

and we want to get output OUT.TXT file format as below:

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,,

who could you help to me? thanks!

Sam

Posted 2012-07-06T09:11:12.197

Reputation: 101

Answers

0

The main problem is that we don't know how much lines with Value we have. Therefore solution is a bit complicated:

sed -n '/^<Value/{s/^<[^>]*>/=/;s/<[^>]*>$//;p}' log1.txt | sort | cat -n |\
sed 's/^\s\+\([0-9]*\)\s\+=/SET DIMM\1=/'

where the first sed greps lines with Value and removes <> tags. After that sort will sort lines, cat -n will enumerate them and last sed will move the line number into DIMM-num-.

With awk it would be pretty close to sed solution:

awk -F'>' '/^<Value/{gsub ("<.*", "", $2);print $2}' log1.txt | sort |\
awk '{print "SET DIMM"NR"="$0}'

rush

Posted 2012-07-06T09:11:12.197

Reputation: 957

I have tried your command line, but no result output, – Sam – 2012-07-07T01:32:15.643

Does your file have the exact format as in example? Try to remove ^ before <Value. – rush – 2012-07-07T08:42:57.803