LinuxQuestions.org
Help answer threads with 0 replies.
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-07-2011, 04:53 PM   #1
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Rep: Reputation: Disabled
gawk scripting question


Hi, I was wondering per chance in anyone could lend a hand with something I am having difficulty doing,

How would I go about writting a script called "display" using gawk for data processing in the script, using a single argument such as, $./display 1998

Outputing each line in a file called cars, and add a new columb of "mileage per year". If the car is older than the specified argument mark with "*" at the beginning of the line.

An example would be,

$./display 1998
*lym 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


Any help would be apreciated, and if you just want to heckle, I implore you to at least make it funny. lol

Any help you would greatly appreciated!
 
Old 12-07-2011, 05:32 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
More input ... which of those output columns is the "newly added"?
Are all cars identified by two words?


Cheers,
Tink
 
2 members found this post helpful.
Old 12-07-2011, 06:12 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
Smells very much like homework to me.

If so, we'll guide you, but we won't do your work for you. So show us what you've done already, and exactly where you're having problems and we'll help you to work through them.

And please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.
 
1 members found this post helpful.
Old 12-07-2011, 07:36 PM   #4
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Tinkster View Post
More input ... which of those output columns is the "newly added"?
Are all cars identified by two words?


Cheers,
Tink
Hi, thanks for your reply!

There is no newly added, all the cars are like that, chevy in one column, malibu in the next column et-cetera. So all the cars are identified by two words, brand and make of the car so to speak. THen the other coulumb will be the year of the car, the next column miles, etc. It's very tricky and have been wrestling with this one for awhile but to no avail as of yet unfortunately.
 
Old 12-07-2011, 07:41 PM   #5
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
Smells very much like homework to me.

If so, we'll guide you, but we won't do your work for you. So show us what you've done already, and exactly where you're having problems and we'll help you to work through them.

And please use [code][/code] tags around your code and data, to preserve formatting and to improve readability.
I'm literally stumped on this one despite putting alot of hours into trying to figue it out. If this was homework, would you think this is a fun assignment for an introductory course to linux, or just slightly over the top? If you don't know how to solve this one I wouldn't think any less of you, there are people I've asked that have been doing linux programming for a long time and they have no clue either, it's very tricky and I can't find any help in any books unfortunately. I only ask for help as a last resort, I prefer to solve problems on my own but this one literally has me stumped right now completely.
 
Old 12-07-2011, 08:21 PM   #6
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
It looks like basic awk job (compare value with a field value, print...), where are you stuck ?
 
Old 12-07-2011, 08:29 PM   #7
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Cedrik View Post
It looks like basic awk job (compare value with a field value, print...), where are you stuck ?
Really? How would you begin the solution for example? If it were basic I would have had it solved by now. lol

This is all I've been able to come up with thus far as kind of a rough sketch.


#!usr/bin/gawk -f
BEGIN{
print " Miles"
print "Make Model Year (000) Price"
print \
"----------------------------------------"
}
{
if $3 < (
if ($1 ~ /ply/) $1= "plymouth"
if ($1 ~ /chev/) $1= "chevolet"
printf "%-10s %-8s %2d %5d $ %8.2f\n,\
$1, $2, $3, $4, $5
}
 
Old 12-07-2011, 09:09 PM   #8
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Cedrik View Post
It looks like basic awk job (compare value with a field value, print...), where are you stuck ?
So in reality you have no idea.

Would it be basic for you to hit a homerun in a major league baseball park agains professional pitching too? lol
 
Old 12-08-2011, 05:30 AM   #9
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 557

Rep: Reputation: 46
Code:
[demo@localhost bin]$ cat cars
F 1920 70 2000
M 1940 50 1000
S 1960 60 1500
V 1960 50 1000

[demo@localhost bin]$ read -p "Year? " year
Year? 1960

[demo@localhost bin]$ cat cars | awk -v YEAR=$year '{if($2 == YEAR){print $0,$4/$3}else{print "*",$0}}'
* F 1920 70 2000
* M 1940 50 1000
S 1960 60 1500 25
V 1960 50 1000 20
[demo@localhost bin]$
Modify the mileage column's value ($4/$3) above as per your requirement/mathematics. If you want mileage irrespective of a year's value matches or not, then carry that piece of code to the end of the "else" block (outside if...else, I mean.)

Note: F 1920 70 2000 = 1: "Car's Brand" 2: "Year" 3: "Km/Per Ltr" 4: "Run Total Kms"

Do you calculations there as per your requirement. But the above code answers your basic question: Asterisk-Mark Lines that do not contain a specified Year and add a Column that says blah blah!

Last edited by devUnix; 12-08-2011 at 05:35 AM.
 
1 members found this post helpful.
Old 12-08-2011, 05:50 AM   #10
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 242Reputation: 242Reputation: 242
Quote:
Originally Posted by Quest ion View Post
So in reality you have no idea.

Would it be basic for you to hit a homerun in a major league baseball park agains professional pitching too? lol
Requirement done in one line:
Code:
awk -v y=1998 '{if($3 < y) printf "*"; print}' cars_file.txt
Look, if you don't like programming, don't waste your time, change activity...
 
2 members found this post helpful.
Old 12-08-2011, 01:03 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by Quest ion View Post
Hi, thanks for your reply!

There is no newly added
Quote:
Outputing each line in a file called cars, and add a new columb of "mileage per year".
How do you make these two statements gel?



Cheers,
Tink
 
Old 12-08-2011, 08:51 PM   #12
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Hello again all, making some progress with your help but more help would be greatly appreciated!
 
Old 12-09-2011, 04:18 AM   #13
jb_gpk
Member
 
Registered: Dec 2010
Distribution: Debian
Posts: 30

Rep: Reputation: 13
What is the 4th field on:
Quote:
volvo s80 1998 102 9850 2.143
why on your attempt you print only 5 fields?

I used a file named car.txt with the content:
Code:
lym 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
I produced an output like this:
Code:
*lym fury 	1970 (73) 2500 $1.825 
chevrolet malibu 	1999 (60) 3000 $5.455 
*ford mustang 	1965 (45) 10000 $1.000 
volvo s80 	1998 (102) 9850 $2.143
using the following script:

Code:
#!/usr/bin/awk -f 
#filename: display.sh

BEGIN{
        ARG_YEAR = y
}
{
        CAR_YEAR = $3;
        if ( ARG_YEAR > CAR_YEAR)
                printf("*");

        #print mark
        if ( $1 ~ /chev/)
                printf("chevrolet ");
        else if  ($1 ~ /ply/)
                printf("plymouth ");
        else
                printf("%s ",$1 );

        printf("%s \t", $2);
        printf("%s ", $3);
        printf("(%s) ", $4 );
        printf("%s ",$5);
        printf("$%s \n",$6);

}
and executing it with the command:
Code:
./display.sh -v y=1979 cars.txt
where -v tells to AWK that you will pass a parameter and y is the year wich you want the older cars to be printed with *.

The entry that I used is right?
 
1 members found this post helpful.
Old 12-09-2011, 05:50 PM   #14
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Hi, I'm trying ot make it function from a file called cars, in the file "cars" it has the following,

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
 
Old 12-09-2011, 06:03 PM   #15
Quest ion
Member
 
Registered: Oct 2011
Posts: 45

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Cedrik View Post
Requirement done in one line:
Code:
awk -v y=1998 '{if($3 < y) printf "*"; print}' cars_file.txt
Look, if you don't like programming, don't waste your time, change activity...
You have some skill; but I don't aspire to be a programmer, some if it is interesting but I am more into IT than CS.

I do find programming to be interesting though and I have just become intorduced ot linux programming only a couple of months ago, so though there is much I have to learn, I don't beleive even excellent programmers "knew it all" within three months. lol

Thank you for your reply though I really appreciate it, well besides the part that tried to discourage me from learning more about linux programming, because I do find some of it kind of interesting and am going ot continue to learn more on my own even after this month.

I'm going to try and tweak that code you came up with so it works for all years I type in, thanks man!!!
 
  


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 04:41 PM
awk or gawk question sharky Programming 4 10-24-2008 02:29 PM
Scripting (?) question ladio Linux - Newbie 3 05-09-2007 01:03 PM
Scripting Question... Darklight451 Linux - Newbie 3 09-23-2004 06:03 PM
gawk question luxpops Programming 1 09-12-2004 05:46 AM


All times are GMT -5. The time now is 08:49 AM.

Main Menu
Advertisement
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