Using gawk to get the ceiling of the fourth column of a pipe delimited txt file
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
I used the statement below in Windows to get the ceiling of a one column txt file but I want to convert the fourth column
gawk "{printf("%s \t %d\n",$0, $0+=$0<0?0:0.9)}"
import csv
import math
with open('roundupo') as _f:
for row in csv.reader(_f, delimiter='|'):
row[3] = math.ceil(float(row[3]))
row[3] = float(row[3])
print(*row, sep='|')
How much effort have you put into understanding that code ?. Just getting another answer from us that you don't understand is not really helping. The pipe can also be specified as separator in awk - see the documentation.
Actually, for such tasks I normally use perl. (Perl-5.)
The original concept of Perl was that it was a "really souped-up awk," and I find that it simply works better for most purposes. Also, of course, it has a vastcontributed library of code that you can simply install and use to do damn-near anything.
In the case at bar, you would simply loop over the file, "split" the line into an array based on the pipe-character, and choose the highest value that you find. (Source-code example not included.)
Nope, the most minor of adjustments to the original code will work fabulously. awk is all that is needed; no need to muddy the waters with loop(s) and/or learning perl.
Nope, the most minor of adjustments to the original code will work fabulously. awk is all that is needed; no need to muddy the waters with loop(s) and/or learning perl.
Yes, the hint for that would be to set the Field Separator and Output Field Separator to the character you need, which is a pipe. See "man awk" for the full reference for the awk language and scroll down to the section "Builtin-variables" You can set both either in a BEGIN { } statement or by appending them to your command line invocation of awk.
It is straight forward but not trivial.
You can assign a value to column $4; this reassambles $0 (the whole line), so OFS must be set.
Define a function, then it's readable and re-usable.
In the function I used int(); the attempt you gave in post #1 would denote sprintf() - but you should add 0.999 at least.
Code:
awk '
BEGIN { FS=OFS="|" }
function ceil(v) { return (v==int(v)) ? v : int(v)+1 }
{ $4=ceil($4); print }
' Input.txt
Last edited by MadeInGermany; 08-30-2017 at 01:15 PM.
If you put variables into a printf format string, a percent in the variables will be interpreted specially; normally you put %s place holders and add them as further arguments.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.