AWK program....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK program....
# 1  
Old 06-10-2010
AWK program....

Dear All,
facing problem to get data in different fields. SO i am using # to get the data..
please refer my code below...


Code:
BEGIN { FS=" " }
{
        if ( $1 == "START" )
        {
        i = i+1;
        SFILENAME[i] = FILENAME
        }
        if ( substr($1,2,8) == "filename" )
                IN_FILENAME[i] = $2
        if ( $2 == "amount" )
                amount[i] = amount[i]"#"$3 ;
        if ( $2 == "decimals" )
                decimals[i] = decimals[i]"#"$3 ;
        if ( $2 == "parameterID" )
                parameterID[i] = parameterID[i]"#"$3 ;
        if ( $2 == "string" )
                string[i] =  string[i]"#"$3 ;
        
}
END{ for (j=1 ; j <=i ; j++)
        printf "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,\n",SFILENAME[j],IN_FILENAME[j],amount[j],decimals[j],parameterID[j],string[j]
}

Output is:
Code:
awk -f get_data.awk test
SFILENAME,IN_FILENAME,amount    ,decimals,parameterID     ,string[j]
test,TMPPMR_3300,#83470000#82570000#900000#900000,#6#6#6#6,#16778220#16778222#ONNET/OFFNET,#3127#OFFNET,
test,TMPPMR_4400,#83470000#82570000#900000#900000,#6#6#6#6,#16778220#16778222#ONNET/OFFNET,#3127#OFFNET,

Output required like following:
Code:
SFILENAME,IN_FILENAME,accountValueBefore,decimals,accountValueAfter,decimals,accumulatedCost,decimals,accountValueDeducted,decimals,parameterID,string     ,parameterID,string,parameterID,string
test  ,TMPPMR_3300,83470000  ,6  ,82570000    ,6     ,900000     ,6      ,900000    ,6    ,16778220   ,ONNET/OFFNET,16778222   ,      ,OFFNET     ,3127
test  ,TMPPMR_4400,83470000  ,6  ,82570000    ,6     ,900000     ,6      ,900000    ,6    ,16778220   ,ONNET/OFFNET,16778222   ,      ,OFFNET     ,3127

Source file:
Code:
START
#input_id 1276078861x001_001436
#output_id
#input_type raw
#output_type asc
#addkey
#source_id unknown
#filename TMPPMR_3300
F B accountValueBefore
F amount 83470000
F decimals 6
F currency 356
.
B accountValueAfter
F amount 82570000
F decimals 6
F currency 356
.
B accumulatedCost
F amount 900000
F decimals 6
F currency 356
.
B accountValueDeducted
F amount 900000
F decimals 6
F currency 356
B ContextParameter
F parameterID 16778220
B parameterValue
F string 3127
.
.
B ContextParameter
F parameterID 16778222
B parameterValue
F unsigned32 2001
.
.
.
B treeDefinedFields
B TreeDefinedField
F parameterID ONNET/OFFNET
B parameterValue
F string OFFNET

START
#input_id 1276078861x001_001436
#output_id
#input_type raw
#output_type asc
#addkey
#source_id unknown
#filename TMPPMR_4400
F amount 83470000
F decimals 6
F currency 356
.
B accountValueAfter
F amount 82570000
F decimals 6
F currency 356
.
B accumulatedCost
F amount 900000
F decimals 6
F currency 356
.
B accountValueDeducted
F amount 900000
F decimals 6
F currency 356
B ContextParameter
F parameterID 16778220
B parameterValue
F string 3127
.
.
B ContextParameter
F parameterID 16778222
B parameterValue
F unsigned32 2001
.
.
.
B treeDefinedFields
B TreeDefinedField
F parameterID ONNET/OFFNET
B parameterValue
F string OFFNET

Moderator's Comments:
Mod Comment It is nice that you use code tags, but please do not mix all your program, input, wrong output and expected output together with descriptive text in just one code tag block in future, ty.

Last edited by zaxxon; 06-10-2010 at 05:57 AM.. Reason: code tag adjustments
# 2  
Old 06-10-2010
Code:
BEGIN { print "SFILENAME,IN_FILENAME,accountValueBefore,decimals,accountValueAfter,decimals,accumulatedCost,decimals,accountValueDeducted,decimals,parameterID,string     ,parameterID,string,parameterID,string" }
       /^START/ {printf "\n%s\t,", FILENAME }
       /^#filename/ {printf "%s\t,",$2}
       /amount/||/decimals/||/parameterID/||/string/ {printf "%s\t,",$3}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If else in awk program

Hi All , I have set of input files with one of the fields as below File 1 -Field 1=BUDGET_CURR_FX File 2 -Field 1=BUDGET_MTH_AVGFX File 3 -Field 1=BUDGET_PREV Now i need to include one extra field in the new file as below Output File 1 -Field 1= BUDGET Field2=CURRENT_FX Output File... (2 Replies)
Discussion started by: Hypesslearner
2 Replies

2. Shell Programming and Scripting

awk program in perl

Hi All, I have an AWK code snippet which I want to use in Perl. How can I achieve the same thing in perl? Here I am searching for a pattern in a file and from that matching line, I am extracting the 3rd column value and storing it in a variable which I later on use this value in a if condition. ... (2 Replies)
Discussion started by: sanzee007
2 Replies

3. Shell Programming and Scripting

Help with awk program

i have two files, one looks like this (file1): novelMiR_892 novelMiR_891, novelMiR_852 novelMiR_893 novelMiR_1661 novelMiR_854 novelMiR_1210 novelMiR_1251 novelMiR_855 novelMiR_1252 novelMiR_897 novelMiR_2336,novelMiR_2335,and the second like this... (5 Replies)
Discussion started by: dietmar13
5 Replies

4. Shell Programming and Scripting

awk program

I have a csv file as below : NAME,5a-6a,6a-7a,7a-8a,8a-9a,9a-10a,10a-11a,11a-12n,12n-1p,13p-14p,14p-15p,15p-16p,16p-17p,17p-18p,18p-19p,19p-20p,20p-21p,21p-22p,22p-23p,11p-12m, TOTALS... (6 Replies)
Discussion started by: deo_kaustubh
6 Replies

5. Shell Programming and Scripting

Problem with awk awk: program limit exceeded: sprintf buffer size=1020

Hi I have many problems with a script. I have a script that formats a text file but always prints the same error when i try to execute it The code is that: { if (NF==17){ print $0 }else{ fields=NF; all=$0; while... (2 Replies)
Discussion started by: fate
2 Replies

6. Shell Programming and Scripting

AWK program

Hi all, I have the following problem and hope someone could help me. I have 184 files, each with 5 columns (c1, c2, c3, c4, c5). I am only interrested in column 5, and would like to paste column 5 from all the 184 files into one file. I have tried the following with two files awk... (20 Replies)
Discussion started by: awkliker
20 Replies

7. UNIX for Dummies Questions & Answers

AWK Program

:(Hi all, I have a doubt in AWK program. I am writing an awk program which accepts a parameter as input and it should display the corresponding details. if the country as "US" it should be displayed as "United States of America" and the country as "IN" India should be displayed. How can I... (2 Replies)
Discussion started by: sivakumar.rj
2 Replies

8. UNIX for Dummies Questions & Answers

AWK program

Hi all, I need to grep the 3 characters from a file, and to fetch the corresponding words to that character. My file is in the following format.. The below text will be in the separate file....say file2.txt ABC This is the first text. DEF This is the second text. GH1 9.8.7890 AB1... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

9. Shell Programming and Scripting

Error in AWK Program

Hi Friends, I need your help. I am not able to execute one awk program .If you can solve the following small program then i can solve other one. $ vi prg #!/bin/awk -f BEGIN { # Print the squares from 1 to 10 the first way i=1; while (i <= 10) { ... (3 Replies)
Discussion started by: bikas_jena
3 Replies

10. UNIX for Dummies Questions & Answers

AWK Program File Help

I have some .dat files that I cannot open and read the data. It is an awk program file, and my question would be to you all is there a way to convert this awk file to ascii text? Thanks (10 Replies)
Discussion started by: ryangfm
10 Replies
Login or Register to Ask a Question