ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
[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!
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!!!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.