LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-09-2011, 05:33 PM   #16
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865

Quote:
Originally Posted by Quest ion View Post
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
 
1 members found this post helpful.
Old 12-09-2011, 08:31 PM   #17
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
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.
 
Old 12-10-2011, 01:58 AM   #18
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by Quest ion View Post
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

Last edited by Tinkster; 12-10-2011 at 04:16 AM.
 
1 members found this post helpful.
Old 12-10-2011, 01:11 PM   #19
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Some of the numbers seem off, I'm going to double check this and then post again!

Last edited by Quest ion; 12-10-2011 at 01:17 PM.
 
Old 12-10-2011, 01:54 PM   #20
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
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.
 
Old 12-10-2011, 02:42 PM   #21
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by Quest ion View Post
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
 
1 members found this post helpful.
Old 12-10-2011, 03:20 PM   #22
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
THANK YOU SO MUCH!!!!!
That was extremely helpful!!!!!

Last edited by Quest ion; 12-10-2011 at 03:22 PM.
 
Old 12-10-2011, 03:32 PM   #23
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Just wanted to thank you again man!!!! Thank you to everyone else who contributed as well!
 
Old 12-10-2011, 06:35 PM   #24
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
After replacing "year=1999", instead with "year=$1" it workings swimmingly!

Now to see if I can beautify the columns a bit!
 
Old 12-10-2011, 07:01 PM   #25
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by Quest ion View Post
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
 
1 members found this post helpful.
Old 12-11-2011, 09:16 PM   #26
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,425

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
Well just so you can see alternatives:
Code:
awk -vy=1999 '$0 = (($3 < y)?"*":"")$0" "$4/(strftime("%Y")-$3)' cars | column -t
 
1 members found this post helpful.
Old 12-12-2011, 07:58 PM   #27
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
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!!

Last edited by Quest ion; 12-12-2011 at 08:12 PM.
 
  


Reply

Tags
awk


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting Question lowcalspam Linux - Newbie 4 06-09-2010 03:41 PM
awk or gawk question sharky Programming 4 10-24-2008 01:29 PM
Scripting (?) question ladio Linux - Newbie 3 05-09-2007 12:03 PM
Scripting Question... Darklight451 Linux - Newbie 3 09-23-2004 05:03 PM
gawk question luxpops Programming 1 09-12-2004 04:46 AM


All times are GMT -5. The time now is 04:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration