Format output using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format output using awk
# 1  
Old 10-16-2013
Format output using awk

Hello all , need help with this ...

Input File

Code:
 
DEV                              %                     POOL
0CB4 FBA     2211300     81792   4  IE  RAID-5(3+1)  R5_EFD100_1                 
     -             -   1805376  82  IF  RAID-1       M2_FC300_1                  
     -             -    132360   6  IS  RAID-6(6+2)  R6_SA2000_1                 
0CB5 FBA     1883700    852732  45  IF  RAID-1       M2_FC300_1                  
     -             -    338112  18  IS  RAID-6(6+2)  R6_SA2000_1                 
0CB6 FBA     1638000     12096   1  IE  RAID-5(3+1)  R5_EFD100_1                 
     -             -    396528  24  IF  RAID-1       M2_FC300_1                  
     -             -    303024  18  IS  RAID-6(6+2)  R6_SA2000_1                 
0CB7 FBA     1605240     16380   1  IE  RAID-5(3+1)  R5_EFD100_1                 
     -             -     50664   3  IF  RAID-1       M2_FC300_1                  
     -             -    100752   6  IS  RAID-6(6+2)  R6_SA2000_1


Output needed
Code:
 
DEV,R5_EFD100_1%:M2_FC300_1%:R6_SA2000_1
0CB4,4:82:6
0CB5,0:45:18  
0CB6,1:24:18
0CB7,1:3:6

Need to format the input using the DEV ,% and POOL Column
If any pool is missing for a DEV , put 0 instead , for example in 2nd row
pool R5_EFD100_1 is not present for 0CB5 , so R5_EFD100_1% would be 0 for that column

Thanks !
# 2  
Old 10-16-2013
Here is an awk solution:

Code:
awk '
NF==8{DEV[++L]=$1}
NF==7{$0=DEV[L]" "$0}
NF==8{POOL[$8];P[$1,$8]=$5}
END {
   printf "DEV"
   for(p in POOL) printf "%s", (i++ ? "%:" : ",") p
   printf "\n"
   for(i=1;i<=L;i++) {
       printf "%s", DEV[i]
           f=0
           for(p in POOL) printf "%s", (f++ ? ":" : ",") P[DEV[i],p]+0
           printf "\n"
   }
}
' infile

# 3  
Old 10-16-2013
Thanks a lot ... it works fine ... just the sequence of pools is different

for the above example .. i get the output in this format
DEV,M2_FC300_1%:R6_SA2000_1:R5_EFD100_1%

The columns below these are fine ... thats not the issue
The pools in the last column may not always come in the same sequence .
Can i "hard code" the pools ... so that the output is always in this format ..

DEV,R5_EFD100_1%:M2_FC300_1%:R6_SA2000_1


Thanks again ....
Thanks
# 4  
Old 10-16-2013
Quote:
Here is an awk solution:



Code:
awk 'NF==8{DEV[++L]=$1}NF==7{$0=DEV[L]" "$0}NF==8{POOL[$8];P[$1,$8]=$5}END { printf "DEV" for(p in POOL) printf "%s", (i++ ? "%:" : ",") p printf "\n" for(i=1;i<=L;i++) { printf "%s", DEV[i] f=0 for(p in POOL) printf "%s", (f++ ? ":" : ",") P[DEV[i],p]+0 printf "\n" }}' infile
Hello Chubler_XL,

Could you please explain it, we will be grateful to you.


Thanks,
R. Singh
# 5  
Old 10-16-2013
Yes we can Hard code columns like this:

Code:
awk '
BEGIN{Pcnt=split("R5_EFD100_1,M2_FC300_1,R6_SA2000_1", POOL, ",")}
NF==8{DEV[++L]=$1}
NF==7{$0=DEV[L]" "$0}
NF==8{P[$1,$8]=$5}
END {
   printf "DEV"
   for(j=1;j<=Pcnt;j++) printf "%s", (i++ ? "%:" : ",") POOL[i]
   printf "\n"
   for(i=1;i<=L;i++) {
       printf "%s", DEV[i]
       f=0
       for(j=1;j<=Pcnt;j++) printf "%s", (f++ ? ":" : ",") P[DEV[i],POOL[j]]+0
       printf "\n"
   }
}
' infile

---------- Post updated at 12:48 PM ---------- Previous update was at 12:35 PM ----------

Explanation of solution in post #2 is:


NF==8{DEV[++L]=$1}
When line has 8 fields append field 1 to array DEV so with example data DEV[1]="0CB4", DEV[2]="0CB5" DEV[3]="0CB6"


NF==7{$0=DEV[L]" "$0}
When line has 7 fields append most recent DEV[] field to beginning of line giving 8 fields in line again.


NF==8{POOL[$8];P[$1,$8]=$5}
Keep list of POOLS used in POOL[] array, Store % value (field 5) in P[] array with index of DEV,POOL so P[0CB4,R5_EFD100_1]=4 P[0CB4,M2_FC300_1]=82


Code:
END {
   printf "DEV"
   for(p in POOL) printf "%s", (i++ ? "%:" : ",") p

When at the end of the input file print headings (i++ ? "%:" : ",") will print comma for first heading and percentage-sign colon for others


Code:
   for(i=1;i<=L;i++) {
       printf "%s", DEV[i]
       f=0
       for(j=1;j<=Pcnt;j++) printf "%s", (f++ ? ":" : ",") P[DEV[i],POOL[j]]+0
       printf "\n"
   }

print DEV code and all values in the P[DEV,POOL] array in order. The +0 ensures missing values appear as zero.
These 2 Users Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to maintain in- and output format with awk

Hi All, I have a data file (myfile.txt) as below: - A H C - A HHH F - AAA HH I The importan point is that the width between the columns are not fixed and the column seperator is space. I wish to change the value of 4th column using awk only when $3 = HH. I can... (4 Replies)
Discussion started by: angshuman
4 Replies

2. Shell Programming and Scripting

Format output in AWK command

hi Friends , I have a file as below s.txt 1~2~~4 2~6~~7 3~8~~9 t.txt 1~2~~4 2~5~8~7 3~8~~7 header for both files is common (2 Replies)
Discussion started by: i150371485
2 Replies

3. Shell Programming and Scripting

Using AWK to format output and email

Hello, I'm a bit stumped, for some reason when using AWK 'print' is not printing the entire date/line. awk '{print "Ticket #: " $1} {print "Queue : " $2} {print "Recieved : " $3} {print "AP Date : " $4} {print "Circuit ID : " $5} {print... (4 Replies)
Discussion started by: ArvinSodhi
4 Replies

4. UNIX for Dummies Questions & Answers

after awk-> format output

hi i have a awk command with several querys.... awk 'FS="|""; print $4, $5, $6...etc.... $4 gives me the date 20120304 $5 is timestamp 101023 I want to format these in 2012.03.04 or 2012/03/04 10:10:23 but have no idea, if this is possible with format-parameters in the awk... (2 Replies)
Discussion started by: Jazzmatazz
2 Replies

5. Shell Programming and Scripting

awk to format an output

awk experts, I have in put file with time stamp followed by "," separated data. same patern continues. The output need time stamp in first columns and data total in 2nd columns. Input file T 9:15 d0,1,3,3 d1,2,1,1 d2,3,1,5 e1,1,1,1 T 9:30 d0,1,1,1 d1,2,3,2 d3,1,2,1... (10 Replies)
Discussion started by: arv_cds
10 Replies

6. Shell Programming and Scripting

awk - format output

Input file1 zone: BAU_SERVER1 C0:50:76:01:C6:20:00:12; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: BAU_SERVER2 C0:50:76:01:C6:20:00:08; 50:06:01:69:3B:20:14:8B; 50:06:01:60:3B:20:14:8B zone: ... (4 Replies)
Discussion started by: greycells
4 Replies

7. Shell Programming and Scripting

scripting/awk help : awk sum output is not comming in regular format. Pls advise.

Hi Experts, I am adding a column of numbers with awk , however not getting correct output: # awk '{sum+=$1} END {print sum}' datafile 2.15291e+06 How can I getthe output like : 2152910 Thank you.. # awk '{sum+=$1} END {print sum}' datafile 2.15079e+06 (3 Replies)
Discussion started by: rveri
3 Replies

8. Shell Programming and Scripting

Awk output format help!!! Urgent!!!!

Hello!! I am capturing and counting certain uniq occurance of certain evet from large log files. below is my output I m getting with my script: No of Messages Date Hour 150 Aug15 1 234 Aug15 2 345 Aug15 3 . ... (4 Replies)
Discussion started by: namodi
4 Replies

9. Shell Programming and Scripting

[need help] output format from awk

hi all, i have a problem with my nawk command output below is the description : nawk $12 == "00008001" { cnt++;cs_cd } END {for(cd in cs_cd) print cd, cs_cd } 2007020814.TDR output : 133 123 desire output: 133,123,.... please advices thank you so much (6 Replies)
Discussion started by: bucci
6 Replies

10. Shell Programming and Scripting

Output in a particular format using AWK

Hi All, I am trying to check if if column 5 is greater than 90. If greater it will print the term in column 6, else if all are within limit, then it will output "Size is within limit". I can't seem to do that with the below code. The output should only be 1 statement of "Size is within the... (4 Replies)
Discussion started by: Raynon
4 Replies
Login or Register to Ask a Question