0

I have a file test.txt contains:

xyz[4.7],default,
xyz[4.3],default, 
xyz[4.4],default, 
xyz[4.6],default, 
harry[1.0],default, 
molly[1.1],default, 
abc[1.4],rome, 
abc[1.2],rome,
abc[1.1],rome, 
abc[1.0],rome, 
ghi[1.0],moon, 

so am trying to place the latest 3 versions in latest.txt and rest of the them into old.txt

am using the below command

awk '{out=a[$1]++ > 2 ? "./temp/old.txt" : "./temp/latest.txt"; print > out}' FS=[

My requirement is the versions am keping as a input variable. lets say some times i need only 2 latest versions sometimes it may be 4

in the above command am making latest 3versions..am hardcoding the variable with "2" in the command.i need it as a variable

Thanks in Advance

Rczone
  • 493
  • 1
  • 5
  • 18
  • 2
    To be clear -- as there are multiple answers given on the question this is linked to -- the appropriate practice is `awk -v var="$var"` to pass `var` from the outer shell to the awk script. – Charles Duffy Jul 28 '15 at 21:35
  • (On a different note -- awk is not part of bash; thus, tagging awk questions with the `bash` tag is inappropriate). – Charles Duffy Jul 28 '15 at 21:37
  • thankyou charles..so in my case if i use: awk awk -v var="$var" '{out=a[$1]++ > var ? "./temp/old.txt" : "./temp/latest.txt"; print > out}' FS=[ will work? – Rczone Jul 28 '15 at 21:38
  • 1
    Inasmuch as it works with `2`, and `var` contains an integer, yes. – Charles Duffy Jul 28 '15 at 21:42

0 Answers0