The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
File conversion gehlnar Shell Programming and Scripting 4 04-17-2009 07:47 AM
File Conversion sridhar_423 Shell Programming and Scripting 2 03-16-2009 04:47 PM
File conversion to ascii mora UNIX for Advanced & Expert Users 4 03-31-2008 12:29 PM
COnversion utility xhtml file to Postscript file dattatray.b SUN Solaris 0 08-25-2005 06:36 AM
file conversion hipo UNIX for Dummies Questions & Answers 1 07-24-2001 09:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-23-2009
gehlnar gehlnar is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 46
File conversion and awk

Hi Everyone,
I am confused with the output of the input file and I am using below command in script to get the expected output.
Also I want to add another condition using logical AND (&&) in place of $2=="L"{$4=0-$4} as $2=="L" && $3=="L" {$4=0-$4} but I am getting some awk error.
Can someone please advise on this please?

Code:
 
cat $FILENAME |grep "^PL"|tr -d '\015'|sort -k1,2|nawk -F'|' 'BEGIN {OFS="~"}$2=="L"{$4=0-$4};$6=="COM"?$6="SHS":$6="FMT";{arr[$1"~"$5"~"$6]+=$4} END {for (i in arr) {print i,arr[i]}}'>$TEMPFILE
Input File 
============
ISIN|BL|STATUS|QTY|SNAME|CLASS
PLKGHM000017|B|L|85000|KGHM|COM
PLPKO0000016|B|L|310000|PKO S.A.|COM
|B|L|0||
PLKGHM000017|L|L|35000|KGHM|COM
PL0000101937|B|L|100000|DS1110|GOV
|L|L|0||
PLKGHM000017|L|L|40000|KGHM|COM
PLPKO0000016|L|L|290000|PKO S.A.|COM
Present Output
==============
PL0000101937~B~L~100000~DS1110~FMT
PLKGHM000017~B~L~85000~KGHM~SHS
PLKGHM000017~L~L~-35000~KGHM~SHS
PLKGHM000017~L~L~-40000~KGHM~SHS
PLPKO0000016~B~L~310000~PKO S.A.~SHS
PLPKO0000016~L~L~-290000~PKO S.A.~SHS
PLPKO0000016~PKO S.A.~SHS~20000
PL0000101937~DS1110~FMT~100000
PLKGHM000017~KGHM~SHS~10000
Expected Output
==============
PLPKO0000016~PKO S.A.~SHS~20000
PL0000101937~DS1110~FMT~100000
PLKGHM000017~KGHM~SHS~10000
  #2 (permalink)  
Old 04-23-2009
devtakh devtakh is offline
Registered User
  
 

Join Date: Oct 2007
Location: Bangalore
Posts: 514
Quote:
Originally Posted by gehlnar View Post
Hi Everyone,
I am confused with the output of the input file and I am using below command in script to get the expected output.
Also I want to add another condition using logical AND (&&) in place of $2=="L"{$4=0-$4} as $2=="L" && $3=="L" {$4=0-$4} but I am getting some awk error.
Can someone please advise on this please?

Code:
 
cat $FILENAME |grep "^PL"|tr -d '\015'|sort -k1,2|nawk -F'|' 'BEGIN {OFS="~"}$2=="L"{$4=0-$4};$6=="COM"?$6="SHS":$6="FMT";{arr[$1"~"$5"~"$6]+=$4} END {for (i in arr) {print i,arr[i]}}'>$TEMPFILE
Input File 
============
ISIN|BL|STATUS|QTY|SNAME|CLASS
PLKGHM000017|B|L|85000|KGHM|COM
PLPKO0000016|B|L|310000|PKO S.A.|COM
|B|L|0||
PLKGHM000017|L|L|35000|KGHM|COM
PL0000101937|B|L|100000|DS1110|GOV
|L|L|0||
PLKGHM000017|L|L|40000|KGHM|COM
PLPKO0000016|L|L|290000|PKO S.A.|COM
Present Output
==============
PL0000101937~B~L~100000~DS1110~FMT
PLKGHM000017~B~L~85000~KGHM~SHS
PLKGHM000017~L~L~-35000~KGHM~SHS
PLKGHM000017~L~L~-40000~KGHM~SHS
PLPKO0000016~B~L~310000~PKO S.A.~SHS
PLPKO0000016~L~L~-290000~PKO S.A.~SHS
PLPKO0000016~PKO S.A.~SHS~20000
PL0000101937~DS1110~FMT~100000
PLKGHM000017~KGHM~SHS~10000
Expected Output
==============
PLPKO0000016~PKO S.A.~SHS~20000
PL0000101937~DS1110~FMT~100000
PLKGHM000017~KGHM~SHS~10000
can you provide a sample of the expected output here? From your post, it is not clear what you are trying to achieve.


cheers,
Devaraj Takhellambam
  #3 (permalink)  
Old 04-23-2009
gehlnar gehlnar is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 46
Hi ,

This is the expected output.

Expected Output
==============
PLPKO0000016~PKO S.A.~SHS~20000
PL0000101937~DS1110~FMT~100000
PLKGHM000017~KGHM~SHS~10000

But if you see my original post I am getting this but along with some other records. Please let me know if you have further query on this.

Cheers,
gehlnar


  #4 (permalink)  
Old 04-23-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
You really don't need 'cat', 'grep' etc.... - awk can do it all itself.

nawk -f geh.awk myFile

geh.awk:
Code:
BEGIN {
  FS="|"
  OFS="~"
}
!/^PL/ { next }
$2=="L" && $3=="L" {$4=0-$4}
{
  $6=($6=="COM")?"SHS":"FMT"
  arr[$1 OFS $5 OFS $6]+=$4
}
END {
  for (i in arr)
    print i,arr[i]
}
But I'm not getting your expected output, although I've tried to follow your code.
  #5 (permalink)  
Old 04-23-2009
gehlnar gehlnar is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 46
Thanks Vgersh , I have correct the code as per your advise.

I am getting expected output if i use awk operation twice i.e. by storing the output before array operation
and on new file if i use array operation I am getting correct output.

But as I was using awk twice , I thought of combining it. Do we something like pipe in awk ?


So it will be like first get this file..

Code:
PL0000101937~B~L~100000~DS1110~FMT
PLKGHM000017~B~L~85000~KGHM~SHS
PLKGHM000017~L~L~-35000~KGHM~SHS
PLKGHM000017~L~L~-40000~KGHM~SHS
PLPKO0000016~B~L~310000~PKO S.A.~SHS
PLPKO0000016~L~L~-290000~PKO S.A.~SHS
and then perform array operation of calculation on this , But My query is can't this be done using one awk operation.
  #6 (permalink)  
Old 04-23-2009
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,116
I don't quite follow what you're doing - sorry.
Could you post the steps and the modified code, please?
  #7 (permalink)  
Old 04-23-2009
gehlnar gehlnar is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 46
vgersh,

Step 1 :Initial File Conversion for basic calculation
Code:
cat INPUTFILE |tr -d '\015'|sort -k1,2|nawk -F'|' 
'BEGIN {OFS="~"}!/^PL/{next}$2=="L" && $3=="L"
{$4=0-$4};$6=="COM"?$6="SHS":$6="FMT"{print $0}'>TEMPFILE
Step 2: Actual calculation to get expected output using above's output of TEMPFILE
Code:
 awk -F'~' '{OFS="~"}{arr[$1OFS$5OFS$6]+=$4} END 
  {for (i in arr) {print i,arr[i]}}'<TEMPFILE>EXPECTEDFILE
I am using above mentioned two steps in a script , Hope you got some idea from it and my idea was to combine
these two steps into one.

Cheers,
gehlnar
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0