[SOLVED] Script to multiply 29th field in CSV file by 1,56
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.
It works on some rows, not all. The outcome is OK in the following rows (the first row is the header). 3, 4, 7, 8, 11, 12, 13, 22, 24, 25, 27, 28, 29 and 30. The rest is left untouched. This time I used a dot, just like you. Please check if you have the same results.
This is not what it should be. I have attached a pdf to clarify what I mean. I made 3 colums: input, output and calc. The latter is the right outcome of the calculation. (Note: forget the comma's, read them as dots)
I am sorry to say, but it does not work completely well. Some of the numbers are not calculated at all. Please also check the previous post with the PDF file.
Maybe awk still gets confused because of the comma's? Is it better to use the "" to check/mark where the fields end?
Check it out on 37.85.
a=59.046+.005=59.051
b=5905.
Therefore the result is 59.05.
The rest is standard awk technique. BTW -N / --use-lc-numeric will use the locale's decimal point as intended. In your case, it seems that you dont want this (need field separators as commas and decimal point as dot).
Well I must say schneidz ... you made me look very hard at my code after your simple solution made me feel like a twit. Unfortunately, your solution has been caught by an issue
I was struggling with to, which is that there are fields represented by - ,,, - this of course has zero quotes in it but do represent fields.
Record 2 has such an entry and hence the value 2.62 is actually the field after the required field.
I have not been able to find a simple solution, short of using a later version:
I used the original file with 7000 records and as far as I could see there were no errors. It took about 2 seconds to process it. Also the 2 decimal rounding that AnanthP mentioned is OK. Although this was not my main concern, as this file is imported into another program and that one handles the rounding too.
Thanks everyone for helping me out. It is great to know there are so many people here that can help you!
Thanks for thinking with me. Why did you create another solution in Ruby? Just a challenge?
I tested it, but get an error: -e:1:in `round': wrong number of arguments (1 for 0) (ArgumentError) from -e:1
Something is wrong with the "round" argument. Maybe it needs an extra space after the word "round"?
Quote:
Originally Posted by grail
Well I am definitely not as adept in C, but here is an alternative I threw together in ruby
However, I am not 100% sure as fairly new to it. It does run just fine for me and I used copy and paste from your post.
I can tell you that there is no spacing issue as the call to round is being used on the float outputed from the previous set of brackets.
Edit: A quick search would tell me that you are using a pre-1.9 version, possibly 1.8.7 which does not support rounding to a specific number of digits, ie it takes no arguments.
I guess what I like about Ruby is its versatility. As you can see from the example you can work on the output of a function immediately as the type can do a particular thing,
ie "scan" returns an array so you can immediately reference elements of the array
I would guess that speed would not be somewhere it has an advantage over something like C, however the ease and speed at which you can cobble code together I quite like.
Yeah chuck up another challenge. As you usual, i can't guarantee a solution but happy to have a go
So, if I want to know something about programming where do I start? C, Ruby or what?
I will start a new thread for the other "challenge", but have to think how to explain what I really want. Maybe it is not even possible. Will let you know when ready.
I have added a new topic: Add URL to a CSV file
Last edited by knilux; 08-03-2012 at 01:36 PM.
Reason: adding name of new topic
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.