maxtektips6
Programmer
Hi all,
With a routine producing a flow like this
header 01
0 X 52
0 G 78
0 T 44
0 B 42
0 Q 70
..
0 A 77
..
0 C 48
0 M 67
0 F 12
..
trailer 01
header 02
0 M 24
0 R 45
..
0 A 45
..
0 C 36
..
trailer 02
etc.
the desired output is like that:
77,42,48
45,,36
etc.
It's basically an extraction of the values in the A, B and C records.
The first solution i've tried was:
But then I noticed the records can come in any order and that sometimes the A and B records can be missing.
This produced an ouput like this in such a situation:
42,77,48
45,36,
Therefore i've tried both of the solutions below but the performance is seriously impacted.
Can these be fine-tuned or the first solution corrected so as to return the correct output?
With a routine producing a flow like this
header 01
0 X 52
0 G 78
0 T 44
0 B 42
0 Q 70
..
0 A 77
..
0 C 48
0 M 67
0 F 12
..
trailer 01
header 02
0 M 24
0 R 45
..
0 A 45
..
0 C 36
..
trailer 02
etc.
the desired output is like that:
77,42,48
45,,36
etc.
It's basically an extraction of the values in the A, B and C records.
The first solution i've tried was:
Code:
routine_producing_data | egrep "A|B|C" | sed 's/^0 //g' | paste -d, - - - |
sed "s/[A-Z]*//g; s/ //g"
But then I noticed the records can come in any order and that sometimes the A and B records can be missing.
This produced an ouput like this in such a situation:
42,77,48
45,36,
Therefore i've tried both of the solutions below but the performance is seriously impacted.
Can these be fine-tuned or the first solution corrected so as to return the correct output?
Code:
routine_producing_data | awk 'BEGIN{a="";b=""}
/A /{a=$3;next}
/B /{b=$3;next}
/C /{print a","b","$3;a="";b=""}
'
Code:
routine_producing_data | perl -ane '$,=",";
@a[0] = @F[2] if (@F[1] eq "A") ;
@a[1] = @F[2] if (@F[1] eq "B") ;
if (@F[1] eq "C") { @a[2] = @F[2] ; print @a ; print "\n" ; @a = () } ;
'