Checking conditions with AWK

Input File1

 
0BB2    2A11   Split,FriApr80625,1507_7RAID5
0BF6    2829   Synchronized,FriJan140653,1507_7RAID5
0BF6    282A   Split,FriApr80625,1507_7RAID5
0C7C    199E   Synchronized,FriJan140653,1507_7RAID5
0C7C    1BCC   Split,FriApr80625,1507_7RAID5
0DCA    0A9B   Synchronized,FriJan140653,1507_7RAID5
0DCA    0AA1   Split,FriApr80625,1507_7RAID5
144B    15A5   Synchronized,FriJan140653,1507_7RAID5
144B    15AD   Split,FriApr80625,1507_7RAID5
0DDD    0AA2   Split,FriApr80625,1507_7RAID5
0DDD    15A6   Synchronized,FriJan140653,1507_7RAID5
0DDD    15AC   Split,FriApr80625,1507_7RAID5

OUTPUT FILE 2

0BB2,0BB2(ERR)
0BF6,0BF6
0C7C,0C7C
0DCA,0DCA
144B,144B 
0DDD,0DDD(ERR)

$1 in FILE1 should be pair of 2 , if its <2 or > 2 then $2 =$1(ERR) , else $2 = $1

OUTPUT FILE3

 
2A11,2A11(GC)
2829,2829(DR)
282A,282A(GC)
199E,199E(DR)
1BCC,1BCC(GC)
0A9B,0A9B(DR)
0AA1,0AA1(GC)
15A5,15A5(DR)
15AD,15AD(GC)
0AA2,0AA2(GC)
15A6,15A6(DR)
15AC,15AC(GC)

If the 3rd field in FILE1 has "date |sed 's/ //g' |awk -F"," '{print substr($1,1,3)substr($2,1,3)substr($2,6,1)}'" in it , $2 = $2(GC)
for example today's date is Friday, April 8, 2011 10:21:47 AM EDT , look for FriApr08 in the 3rd field

If 3rd field does not have the above date condition then $2 =$2(DR)

Thanks

nawk '{A[$1]++}END{for(i in A) print i,(A%2?i FS "(ERR)":i)}' file1 | tee file2

For second condition:

 
to_day=`date | awk '{ print$1$2$3}'`
awk -v dat=$to_day '$3 ~ dat { $0=$2","$2"(GC)";print;next} {$0=$2","$2"(DR)";print}' OFS="," input_file

FYI, you can also use

date '+%a%b%e' | sed 's/ //g'

instead of your

date |sed 's/ //g' |awk -F"," '{print substr($1,1,3)substr($2,1,3)substr($2,6,1)}'

To make all this stuff in a single shot!

awk -v dat=$to_day '{ A[$1]++ }
          $3 ~ dat { $0=$2","$2"(GC)";print > "file3" ; next }
          {$0=$2","$2"(DR)";print > "file3" }
          END{for(i in A) print i,(A%2?i OFS "(ERR)":i)}' OFS="," input_file
to_day=$(date '+%a%b%e' | sed 's/ //g')
nawk -v dat=$to_day '{x=($3~dat)?"(GC)":"(DR)";$0=$2","$2 x}1' file1 > file3

This alone should be OK :slight_smile:

to_day=$(date '+%a%b%0e')

One shot :

to_day=$(date '+%a%b%e' | sed 's/ //g')
awk -v dat=$to_day '{A[$1]++;x=($3~dat)?"(GC)":"(DR)";print $2","$2 x >"file3"}END{for(i in A) print i,(A%2?i FS "(ERR)":i) >"file2"}' file1

use nawk if on SunOS / Solaris

@ Panyam :slight_smile:

to_day=$(date '+%a%b%0e')

Will give (on Linux)

FriApr08

Whereas in the file the format is

FriApr8

By the way, this '0' option is not standard ...unknown on my solaris for example(see below) (... but ok it works on Linux ).

# date '+%a%b%0e'
FriApr%0e
# date '+%a%b%e'
FriApr 8

Thanks for the quick response Guys !

Thanks ctsgnb. How ever, I got a different result in my system ( might be OS dependent)!!!!

 
date '+%a%b%0e'
FriApr8
uname -a
HP-UX avalon B.11.11 U 9000/800 3547052374 unlimited-user license

have another quick one ....

If i have a file with lines like this

 
 
2917    0DDD   RDF1+TDEV       75(5)   2917_3RAID5     33      05F:0_10F:0     10000000c961408f    0A9B   0AA1

and a reference file like this ...

 
0A9B,0A9B(DR)
0AA1,0AA1(GC)
0DDD,0DDD(ERR)

I want to replace column 2 ,9 and 10 with column 2 value of reference file if it matches to column 1 of reference file . If there is no match ..keep the same value

Output

 
2917    0DDD(ERR)   RDF1+TDEV       75(5)   2917_3RAID5     33      05F:0_10F:0 10000000c961408f    0A9B(DR)   0AA1(GC)

thx

awk 'NR==FNR{$1=$1;A[$1]=$2}NR!=FNR{FS="  *";for(i=2;i<=NF;i++) {$i=(A[$i])?A[$i]:$i""};print}' FS=, referencefile FS=" " inputfile

Thanks a lot ...