LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   gawk scripting question (https://www.linuxquestions.org/questions/programming-9/gawk-scripting-question-917623/)

Tinkster 12-09-2011 05:33 PM

Quote:

Originally Posted by Quest ion (Post 4546225)
Hi, I'm trying ot make it function from a file called cars, in the file "cars" it has the following,
Code:

plym    fury    1970    73      2500
chevy  malibu  1999    60      3000
ford    mustang 1965    45      10000
volvo  s80    1998    102    9850
ford    thundbd 2003    15      10500
chevy  malibu  2000    50      3500
bmw    325i    1985    115    450
honda  accord  2001    30      6000
ford    taurus  2004    10      17000
toyota  rav4    2002    180    750
chevy  impala  1985    85      1550
ford    explor  2003    25      9500


Which brings us back to the question where the newly added 6th column
with the floating point numbers came from ... 3rd time I'm asking about
the same thing in slightly different words.
Code:

*plym  fury    1970    73      2500 1.825
chevy  malibu  1999    60      3000 5.455
*ford  mustang 1965    45    10000 1.000
volvo  s80    1998  102      9850 2.143


And please wrap your file content in code - tags to preserve spacing,
makes it much easier to read.



Cheers,
Tink

Quest ion 12-09-2011 08:31 PM

Quote:

Originally Posted by Tinkster (Post 4546244)
Which brings us back to the question where the newly added 6th column
with the floating point numbers came from ... 3rd time I'm asking about
the same thing in slightly different words.
Code:

*plym  fury    1970    73      2500 1.825
chevy  malibu  1999    60      3000 5.455
*ford  mustang 1965    45    10000 1.000
volvo  s80    1998  102      9850 2.143


And please wrap your file content in code - tags to preserve spacing,
makes it much easier to read.



Cheers,
Tink

My bad! The car file is the input file, and the script is supposed to output each line in the cars file and also add a new column of "miles per year" which is the 6th column. Then if the car is older than the specified argument, mark with a "*" at the beginning of the line.

I've spent about five hours on this today and still having no luck with that part.

Tinkster 12-10-2011 01:58 AM

Quote:

Originally Posted by Quest ion (Post 4546314)
My bad! The car file is the input file, and the script is supposed to output each line in the cars file and also add a new column of "miles per year" which is the 6th column. Then if the car is older than the specified argument, mark with a "*" at the beginning of the line.

I've spent about five hours on this today and still having no luck with that part.

I looked at the year, and the other two columns, and your "miles per year", and I can't
make those figures produce the result you computed manually in your desired output. So
which columns did you use to make the floats up?

With your input I can't make up an algorithm ....


Cheers,
Tink

Quest ion 12-10-2011 01:11 PM

Some of the numbers seem off, I'm going to double check this and then post again!

Quest ion 12-10-2011 01:54 PM

Ok, so 4th column is miles, 5th column is price and and the proposed 6th is to be calculated as miles/age of the car for miles per year.

Tinkster 12-10-2011 02:42 PM

Quote:

Originally Posted by Quest ion (Post 4546716)
Ok, so 4th column is miles, 5th column is price and and the proposed 6th is to be calculated as miles/age of the car for miles per year.

Code:

awk -v year=1999 '$3<year{printf "*"}{print $0" "$4/(strftime("%Y")-$3)}' cars
*plym    fury    1970    73      2500 1.78049
chevy  malibu  1999    60      3000 5
*ford    mustang 1965    45      10000 0.978261
*volvo  s80    1998    102      9850 7.84615
ford    thundbd 2003    15      10500 1.875
chevy  malibu  2000    50      3500 4.54545
*bmw    325i    1985    115        450 4.42308
honda  accord  2001    30      6000 3
ford    taurus  2004    10      17000 1.42857
toyota  rav4    2002    180        750 20
*chevy  impala  1985    85      1550 3.26923
ford    explor  2003    25      9500 3.125

Formatting the output to look pretty I leave as an exercise to you :o)


Cheers,
Tink

Quest ion 12-10-2011 03:20 PM

THANK YOU SO MUCH!!!!!
That was extremely helpful!!!!!

Quest ion 12-10-2011 03:32 PM

Just wanted to thank you again man!!!! Thank you to everyone else who contributed as well!

Quest ion 12-10-2011 06:35 PM

After replacing "year=1999", instead with "year=$1" it workings swimmingly!

Now to see if I can beautify the columns a bit!

Tinkster 12-10-2011 07:01 PM

Quote:

Originally Posted by Quest ion (Post 4546831)
After replacing "year=1999", instead with "year=$1" it workings swimmingly!

Now to see if I can beautify the columns a bit!

Cool. Hint: use printf and TABs (\t) ;}


Cheers,
Tink

grail 12-11-2011 09:16 PM

Well just so you can see alternatives:
Code:

awk -vy=1999 '$0 = (($3 < y)?"*":"")$0" "$4/(strftime("%Y")-$3)' cars | column -t

Quest ion 12-12-2011 07:58 PM

Awesome!!! Thanks a ton Tinkster!!!

Thank to you too grail! That's kind of interesting as well, it's kind of cool how there are multiple ways of solving the same problem with Linux! Learned something new with column -t as well! Thanks man!!


All times are GMT -5. The time now is 06:56 PM.