Sponsored Content
Full Discussion: Help on looping using awk
Top Forums Shell Programming and Scripting Help on looping using awk Post 302884998 by jeffreybsu on Thursday 23rd of January 2014 02:19:43 AM
Old 01-23-2014
Help on looping using awk

I have the data like this:
Code:
PONUMBER,SUPPLIER,LINEITEM,SPLITLINE,LINEAMOUNT,CURRENCY
IR5555,Supplier1,1,1,83.1,USD
IR5555,Supplier1,1,3,40.4,USD
IR5555,Supplier1,1,6,54.1,USD
IR5555,Supplier1,1,8,75.1,USD

IR5556,Supplier2,1,1,41.1,USD
IR5556,Supplier2,1,3,43.1,USD

IR5557,Supplier3,1,1,41.1,USD
IR5557,Supplier3,2,3,43.1,USD

Basically what I needed to do was arrange the splitline field in the correct order if the lineitems are of the same value and on the same PONUMBER. for reference please see below:
Code:
PONUMBER,SUPPLIER,LINEITEM,SPLITLINE,LINEAMOUNT,CURRENCY
IR5555,Supplier1,1,1,83.1,USD
IR5555,Supplier1,1,2,40.4,USD
IR5555,Supplier1,1,3,54.1,USD
IR5555,Supplier1,1,4,75.1,USD

IR5556,Supplier2,1,1,41.1,USD
IR5556,Supplier2,1,2,34.1,USD

IR5557,Supplier3,1,1,12.1,USD
IR5557,Supplier3,2,1,78.1,USD

I have tried to use this code:
Code:
awk -F',' 'NR==1{print;next}{a[$1","$2","$3]++;b[$1","$2","$3]=$5","$6};END
 {for(x in a) {for(i=1; i<=a[x]; i++) {print x","i","b[x]}}}' test.csv

which corrects the splitline numbering but I have overlooked the amount, now my main problem is the lineamount is not correct, it only gets one value and repeats it. please see below:
Code:
PONUMBER,SUPPLIER,LINEITEM,SPLITLINE,LINEAMOUNT,CURRENCY
IR5555,Supplier1,1,1,75.1,USD
IR5555,Supplier1,1,2,75.1,USD
IR5555,Supplier1,1,3,75.1,USD
IR5555,Supplier1,1,4,75.1,USD

IR5557,Supplier3,1,1,12.1,USD
IR5557,Supplier3,2,1,78.1,USD

IR5556,Supplier2,1,1,34.1,USD
IR5556,Supplier2,1,2,34.1,USD

I would really appreciate guys if you could help me in the lineamount issue.
Thanks!

Last edited by jeffreybsu; 01-23-2014 at 03:25 AM.. Reason: corrected output data
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Looping in awk

Can somebody give me a cleaner way of writing the following script. I was thinking that I could use a loop in the awk statement. It works fine the way it is but I just want the script to be cleaner. #!/usr/bin/sh for r in 0 1 2 3 4 5 6 do DAY=`gdate --date="${r} days ago" +%m\/%d\/%y`... (3 Replies)
Discussion started by: keelba
3 Replies

2. Shell Programming and Scripting

Awk: looping problem!

I am having a problem with awk when I run it with a loop. It works perfectly when I echo a single line from the commandline. For example: echo 'MFG009 9153852832' | awk '$2 ~ /^0-9]$/{print $2}' The Awk command above will print field 2 if field 2 matches 10 digits, but when I run the loop... (5 Replies)
Discussion started by: cstovall
5 Replies

3. Shell Programming and Scripting

looping and awk/sed help

I am pretty new to this, but imagine what I am trying to do is possible iI am trying to make an automated DB comparison tool that selects all columns in all tables and compares them to the same thing in another DB. anyway I have created 2 files to help with this the first file is a... (13 Replies)
Discussion started by: Zelp
13 Replies

4. Shell Programming and Scripting

Urgent - Looping using AWK

Hi I have a file which is having following text. The file is in a tabular form with 5 fields. i.e field1, field2 ..... field5 are its columns and there are many rows in it say COUNT is the number of rows Field 1 Field2 Field3 Field4 Field5 ------- ------- ... (8 Replies)
Discussion started by: skyineyes
8 Replies

5. UNIX for Dummies Questions & Answers

Help with AWK looping

I'm trying to parse a configuration text file using awk. The following is a sample from the file I'm searching. I can retrieve the formula and recipe names easily but now I want to take it one step farther. In addition to the formula name, I would like to also get the value of the attribute... (6 Replies)
Discussion started by: new2awk
6 Replies

6. Shell Programming and Scripting

looping in awk

How do I remove last comma? echo "xx yy zz" | awk 'BEGIN{FS=" "}{for (i=1; i<=NF; i++) printf "%s,", $i}'output: xx,yy,zz, required output: xx,yy,zz or (ideally!): xx, yy & zz many thanks in advance! (4 Replies)
Discussion started by: euval
4 Replies

7. Shell Programming and Scripting

Looping within the elements of a file using awk

Hi all, I have a file containing 5000 rows and 4 columns. I need to do a loop within the rows based on the values of column 3. my sample data is formatted like the ones below: what i need to do is to make a loop that will allow me to plot the values of x,y,values corresponding to month 1 to month... (10 Replies)
Discussion started by: ida1215
10 Replies

8. Shell Programming and Scripting

AWK looping over 2 variables

I would like to loop over variables i and j consecutively, { a = -6.7 b = 7.0 c =0.1 { for (i = 0; i<=(b-a)/c; i++) for (j = 1; j<=(b-a)/c; j++) '$1<=(a+j*c)&&$1>=(a+i*c)' FILENAME > output_j '{print $2}' output_j > output_j_f } I essentially want to print the range of $1... (9 Replies)
Discussion started by: chrisjorg
9 Replies

9. Shell Programming and Scripting

Looping through pairs of files with awk

Hi all, please help me construct the command. i want to loop through all files named bam* and bed*. My awk works for a particular pair but there are too many pairs to do manually. I have generated multiple files in a folder in a given pattern. The files are named like bam_fixed1.bam... (2 Replies)
Discussion started by: newbie83
2 Replies

10. Shell Programming and Scripting

awk nested looping?

I am trying to parse a text file and send its output to another file but I am having trouble conceptualizing how I am supposed to do this in awk. The text file has a organization like so: Name Date Status Location (city, state, zip fields) Where each of these is on a separate line in... (1 Reply)
Discussion started by: kellyanneghj
1 Replies
Moose::Cookbook::Roles::Comparable_CodeReuse(3) 	User Contributed Perl Documentation	   Moose::Cookbook::Roles::Comparable_CodeReuse(3)

NAME
Moose::Cookbook::Roles::Comparable_CodeReuse - Using roles for code reuse VERSION
version 2.0604 SYNOPSIS
package Eq; use Moose::Role; requires 'equal_to'; sub not_equal_to { my ( $self, $other ) = @_; not $self->equal_to($other); } package Comparable; use Moose::Role; with 'Eq'; requires 'compare'; sub equal_to { my ( $self, $other ) = @_; $self->compare($other) == 0; } sub greater_than { my ( $self, $other ) = @_; $self->compare($other) == 1; } sub less_than { my ( $self, $other ) = @_; $self->compare($other) == -1; } sub greater_than_or_equal_to { my ( $self, $other ) = @_; $self->greater_than($other) || $self->equal_to($other); } sub less_than_or_equal_to { my ( $self, $other ) = @_; $self->less_than($other) || $self->equal_to($other); } package Printable; use Moose::Role; requires 'to_string'; package US::Currency; use Moose; with 'Comparable', 'Printable'; has 'amount' => ( is => 'rw', isa => 'Num', default => 0 ); sub compare { my ( $self, $other ) = @_; $self->amount <=> $other->amount; } sub to_string { my $self = shift; sprintf '$%0.2f USD' => $self->amount; } DESCRIPTION
Roles have two primary purposes: as interfaces, and as a means of code reuse. This recipe demonstrates the latter, with roles that define comparison and display code for objects. Let's start with "Eq". First, note that we've replaced "use Moose" with "use Moose::Role". We also have a new sugar function, "requires": requires 'equal_to'; This says that any class which consumes this role must provide an "equal_to" method. It can provide this method directly, or by consuming some other role. The "Eq" role defines its "not_equal_to" method in terms of the required "equal_to" method. This lets us minimize the methods that consuming classes must provide. The next role, "Comparable", builds on the "Eq" role. We include "Eq" in "Comparable" using "with", another new sugar function: with 'Eq'; The "with" function takes a list of roles to consume. In our example, the "Comparable" role provides the "equal_to" method required by "Eq". However, it could opt not to, in which case a class that consumed "Comparable" would have to provide its own "equal_to". In other words, a role can consume another role without providing any required methods. The "Comparable" role requires a method, "compare": requires 'compare'; The "Comparable" role also provides a number of other methods, all of which ultimately rely on "compare". sub equal_to { my ( $self, $other ) = @_; $self->compare($other) == 0; } sub greater_than { my ( $self, $other ) = @_; $self->compare($other) == 1; } sub less_than { my ( $self, $other ) = @_; $self->compare($other) == -1; } sub greater_than_or_equal_to { my ( $self, $other ) = @_; $self->greater_than($other) || $self->equal_to($other); } sub less_than_or_equal_to { my ( $self, $other ) = @_; $self->less_than($other) || $self->equal_to($other); } Finally, we define the "Printable" role. This role exists solely to provide an interface. It has no methods, just a list of required methods. In this case, it just requires a "to_string" method. An interface role is useful because it defines both a method and a name. We know that any class which does this role has a "to_string" method, but we can also assume that this method has the semantics we want. Presumably, in real code we would define those semantics in the documentation for the "Printable" role. (1) Finally, we have the "US::Currency" class which consumes both the "Comparable" and "Printable" roles. with 'Comparable', 'Printable'; It also defines a regular Moose attribute, "amount": has 'amount' => ( is => 'rw', isa => 'Num', default => 0 ); Finally we see the implementation of the methods required by our roles. We have a "compare" method: sub compare { my ( $self, $other ) = @_; $self->amount <=> $other->amount; } By consuming the "Comparable" role and defining this method, we gain the following methods for free: "equal_to", "greater_than", "less_than", "greater_than_or_equal_to" and "less_than_or_equal_to". Then we have our "to_string" method: sub to_string { my $self = shift; sprintf '$%0.2f USD' => $self->amount; } CONCLUSION
Roles can be very powerful. They are a great way of encapsulating reusable behavior, as well as communicating (semantic and interface) information about the methods our classes provide. FOOTNOTES
(1) Consider two classes, "Runner" and "Process", both of which define a "run" method. If we just require that an object implements a "run" method, we still aren't saying anything about what that method actually does. If we require an object that implements the "Executable" role, we're saying something about semantics. AUTHOR
Moose is maintained by the Moose Cabal, along with the help of many contributors. See "CABAL" in Moose and "CONTRIBUTORS" in Moose for details. COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by Infinity Interactive, Inc.. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. perl v5.16.2 2012-09-19 Moose::Cookbook::Roles::Comparable_CodeReuse(3)
All times are GMT -4. The time now is 07:00 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy