![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 08: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 01:29 PM |
| COnversion utility xhtml file to Postscript file | dattatray.b | SUN Solaris | 0 | 08-25-2005 07:36 AM |
| file conversion | hipo | UNIX for Dummies Questions & Answers | 1 | 07-24-2001 10:24 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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
|
|
||||
|
Quote:
cheers, Devaraj Takhellambam |
|
||||
|
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 |
|
||||
|
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. |
|
||||
|
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 |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|