awk with fields select?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk with fields select?
# 1  
Old 02-23-2012
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
Code:
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.
Code:
1329515230||||||Available|chicken||1329519195
1329520802||||||Available|cat||1330038028
1329524298||||||In Use|bear||nowtime


I have problem like we have 2 In Use
Code:
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.
# 2  
Old 02-24-2012
How about this:

Code:
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

# 3  
Old 02-24-2012
Hi Chubler_XL,
I replaced nowtime= current time in epoch
Code:
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.
# 4  
Old 02-24-2012
Quote:
Originally Posted by sabercats
Hi Chubler_XL,
I replaced nowtime= current time in epoch
Code:
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).
Code:
awk -v nowtime="$shelldatevar" '{print nowtime}' file

just enhance this with your code.
Cheers,
Ranga:-)
# 5  
Old 02-24-2012
Quote:
Originally Posted by rangarasan
Another thing, in awk we should not use $ for user defined variables(like you used inside awk $nowtime).
$ does indeed have a purpose in awk, but it's not to define variables -- it's to select columns.

Code:
$ 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

$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk move select fields to match file prefix in two directories

In the awk below I am trying to use the file1 as a match to file2. In file2 the contents of $5,&6,and $7 (always tab-delimited) and are copied to the output under the header Quality metrics. The below executes but the output is empty. I have added comments to help and show my thinking. Thank you... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

3. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

4. Shell Programming and Scripting

Select records and fields

Hi All I would like to modify a file like this: >antax gioq21 tris notes abcdefghij klmnopqrs >betax gion32 ter notes2 tuvzabcdef ahgskslsooin this: >tris abcdefghij klmnopqrs >ter tuvzabcdef ahgskslsoo So, I would like to remove the first two fields(and output field 3) in record... (4 Replies)
Discussion started by: giuliangiuseppe
4 Replies

5. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

6. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

7. Shell Programming and Scripting

select lines with certain values on certain fields with awk

I need a awk command to select from a log-file only the lines that have on the 2nd field (considering "|" separator) one of the values 10.216.22.XX or 10.216.22.YY or 10.216.22.ZZ and on the 4th field only values that contain strictly digits. I want the command to work parsing the file only once (I... (2 Replies)
Discussion started by: black_fender
2 Replies

8. UNIX for Dummies Questions & Answers

Select Distinct on multiple fields

How do I create a script that provides a count of distinct values of a particular field in a file utilizing commonly available UNIX commands (sh or awk)? Field1|Field2|Field3|Field4 AAA|BBB|CCC|DDD 111|222|333|777 AAA|EEE|ZZZ|EEE 111|555|333|444 AAA|EEE|CCC|DDD 111|222|555|444 For... (2 Replies)
Discussion started by: Refresher
2 Replies

9. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

10. Shell Programming and Scripting

printing select fields in awk

Hi, I want to print certain fields from my data file depending on certain conditions. Somebody pls let me know how to send it to awk. The command below is the one which I want to use in a shell script and this prints fine cat ./datafile.dat | grep -i $SEARCH_STR | awk -F: '{ print $1 $2 $3... (5 Replies)
Discussion started by: maverix
5 Replies
Login or Register to Ask a Question