LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 08-28-2017, 03:04 PM   #1
junzph
LQ Newbie
 
Registered: Aug 2017
Posts: 3

Rep: Reputation: Disabled
Using gawk to get the ceiling of the fourth column of a pipe delimited txt file


Input.txt
ABC111|8/28/2017|15:00|4.00|Z000
ABC222|8/28/2017|15:00|4.01|Z001
ABC333|8/28/2017|15:00|4.05|Z002
ABC444|8/28/2017|15:00|4.06|Z003
ABC555|8/28/2017|15:00|4.09|Z004

Output.txt
ABC111|8/28/2017|15:00|4.00|Z000
ABC222|8/28/2017|15:00|5.00|Z001
ABC333|8/28/2017|15:00|5.00|Z002
ABC444|8/28/2017|15:00|5.00|Z003
ABC555|8/28/2017|15:00|5.00|Z004

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)}"

Please help. Thank you in advance
 
Old 08-28-2017, 07:02 PM   #2
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 633

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Code:
ABC555|8/28/2017|15:00|4.09|Z004
-> 
ABC555|8/28/2017|15:00|5.00|Z004
If I understand right.. you want to take the 4th column and round up?

I don't know gawk, but with python I'd do the below:
Demo of code (jupyter_notebook): https://gist.github.com/anonymous/a0...rt_value-ipynb

Code:
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='|')

Last edited by Sefyir; 08-28-2017 at 07:10 PM.
 
Old 08-28-2017, 07:36 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,713

Rep: Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549
Quote:
Originally Posted by junzph View Post
Please help.
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.
 
Old 08-29-2017, 07:22 AM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,154
Blog Entries: 4

Rep: Reputation: 3232Reputation: 3232Reputation: 3232Reputation: 3232Reputation: 3232Reputation: 3232Reputation: 3232Reputation: 3232Reputation: 3232Reputation: 3232Reputation: 3232
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 vast contributed 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.)
 
Old 08-29-2017, 07:32 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,713

Rep: Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549Reputation: 3549
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.
 
Old 08-29-2017, 07:53 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
Quote:
Originally Posted by syg00 View Post
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.
 
Old 08-29-2017, 08:05 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,510

Rep: Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552
you can specify the delimiter in awk, just google http://www.theunixschool.com/2012/07...iles-with.html

Last edited by pan64; 08-29-2017 at 08:06 AM.
 
Old 08-30-2017, 01:14 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,745

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
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.
 
1 members found this post helpful.
Old 09-05-2017, 02:50 PM   #9
junzph
LQ Newbie
 
Registered: Aug 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
I finally got it going by using the statement below

gawk -F"|" "{printf($1 ""|"" $2 ""|"" $3 ""|"" "%d.00"|""$5" \n", $4+=$4<0?0:0.9)}" temp.txt

and running it on a command prompt, I get a result,

ABC111|8/28/2017|15:00|4.00|Z000
ABC222|8/28/2017|15:00|4.00|Z001
ABC333|8/28/2017|15:00|4.00|Z002
ABC444|8/28/2017|15:00|4.00|Z003
ABC555|8/28/2017|15:00|4.00|Z004

but when I save it into a batch file i.e. test.bat, I get an error that shows

gawk: {printf(1 "|" $2 "|" $3 "|" "0.9)}
gawk: ^ unternimated string

Any advise please. Thanks.
 
Old 09-05-2017, 02:51 PM   #10
junzph
LQ Newbie
 
Registered: Aug 2017
Posts: 3

Original Poster
Rep: Reputation: Disabled
By the way the caret sign ^ unterminated string is pointing at the double quotes just before the 0.9
 
Old 09-05-2017, 03:34 PM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,745

Rep: Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791Reputation: 791
An awk scripts is better put in 'ticks'
Code:
gawk -F"|" '{printf "%s|%s|%s|%d.00|%s\n", $1, $2, $3, $4+=$4<0?0:0.9, $5}' temp.txt
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.
 
  


Reply


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
[SOLVED] NEWBIE: grep/awk to get fourth column in file? hristo77 Programming 5 11-26-2015 09:43 AM
get the data from pipe delimited file using awk jone kim Linux - General 6 08-21-2014 12:56 PM
How to ignore Pipe in Pipe delimited file? rohit_shinez Programming 29 08-13-2013 11:53 PM
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
column re-alignment - space delimited to comma delimited hattori.hanzo Linux - Newbie 9 03-05-2009 12:54 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:44 PM.

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
Open Source Consulting | Domain Registration