Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question

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}'
share|improve this answer
    
I have tried your command line, but no result output, – Sam Jul 7 '12 at 1:32
    
Does your file have the exact format as in example? Try to remove ^ before <Value. – rush Jul 7 '12 at 8:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .