awk with fields select?

If i have a log file record.txt, with 10 fields

  • First field is datetime
  • 7th field is status
  • 8th filed is name
  • The last field (10th) is epoch time of the first field
02/17/2012 1:47 PM||||||In Use|chicken||1329515230
02/17/2012 2:53 PM||||||Available|chicken||1329519195
02/17/2012 3:20 PM||||||In Use|cat||1329520802
02/17/2012 3:22 PM||||||In Use|cat||1329520957
02/17/2012 4:18 PM||||||In Use|bear||1329524298
02/23/2012 3:00 PM||||||Available|cat||1330038028

How do i create a new flat file record combine.txt which has 10 new fields

  • first field is datetime in epoch "In Use" of the name like chicken
    -2nd fields to 10 fields is from "Available" of that name like chicken.
1329515230||||||Available|chicken||1329519195
1329520802||||||Available|cat||1330038028
1329524298||||||In Use|bear||nowtime

I have problem like we have 2 In Use

02/17/2012 3:20 PM||||||In Use|cat||1329520802
02/17/2012 3:22 PM||||||In Use|cat||1329520957

How it select and use only the earliest one?
and if In Use but not available yet then it should be now time?

Thanks for you input.

How about this:

awk -F\| '
  $7 == "In Use" && not ($8 in U) {
      U[$8]=$10;
      A[$8]="nowtime"
  }
  $7 == "Available" {A[$8]=$10}
  END {for(id in U)
    print U[id] "||||||" (A[id]=="nowtime"?"Available":"In Use") "|" id "||" A[id] }' infile

Hi Chubler_XL,
I replaced nowtime= current time in epoch

awk -F\| '
  $7 == "In Use" && not ($8 in U) {
      U[$8]=$10;
       nowtime=$(date +%s)
      A[$8]=$nowtime;
     
  }
  $7 == "Available" {A[$8]=$10}
  END {for(id in U)
print U[id] "||||||" (A[id]=="$nowtime"?"Available":"In Use") "|" id "||" A[id] }' record.txt

--> i got the result
Unmatched .

Do you know what was wrong? Thanks.

you have to pass time variable by -v option in awk.
Another thing, in awk we should not use $ for user defined variables(like you used inside awk $nowtime).

awk -v nowtime="$shelldatevar" '{print nowtime}' file

just enhance this with your code.
Cheers,
Ranga:-)

$ does indeed have a purpose in awk, but it's not to define variables -- it's to select columns.

$ echo a b c | awk '{ print $1 }

a

$ echo a b c | awk -v VAR=2 '{ print VAR }'

2

$ echo a b c | awk -v VAR=2 '{ print $VAR }'

b

$