[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.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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)
Check it out on 37.85.
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"?
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