Multi condition mapping


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Multi condition mapping
# 1  
Old 05-27-2013
Multi condition mapping

input
Code:
ABC179W_Down,TM,startBaseCut40WMachineDown,startBaseCut60WMachineDown,licenseStateNum40WMachineDown,licenseStateNum60WMachineDown
ABC179W_Down,MachineBFunction=1,3,0,1,0
BCD187W_Lack_2_Time,TM,startBaseCut100WMachineDown,startBaseCut120WMachineDown,startBaseCut40WMachineDown,startBaseCut60WMachineDown,startBaseCut80WMachineDown,licenseStateNum100WMachineDown,licenseStateNum120WMachineDown,licenseStateNum40WMachineDown,licenseStateNum60WMachineDown,licenseStateNum80WMachineDown
BCD187W_Lack_2_Time,MachineBFunction=1,0,0,3,0,0,0,0,1,0,0
ADB650W_AXE_Aussie,TM,startBaseCut40WMachineDown,startBaseCut60WMachineDown,licenseStateNum40WMachineDown,licenseStateNum60WMachineDown
ADB650W_AXE_Aussie,MachineBFunction=1,0,0,0,1
GHS052W_Cambod_Rush,TM,startBaseCut100WMachineDown,startBaseCut120WMachineDown,startBaseCut40WMachineDown,startBaseCut60WMachineDown,startBaseCut80WMachineDown,licenseStateNum100WMachineDown,licenseStateNum120WMachineDown,licenseStateNum40WMachineDown,licenseStateNum60WMachineDown,licenseStateNum80WMachineDown
GHS052W_Cambod_Rush,MachineBFunction=1,0,0,3,0,0,0,0,1,0,0

expected output
Code:
name,TM,startBaseCut100WMachineDown,startBaseCut120WMachineDown,startBaseCut40WMachineDown,startBaseCut60WMachineDown,startBaseCut80WMachineDown,licenseStateNum100WMachineDown,licenseStateNum120WMachineDown,licenseStateNum40WMachineDown,licenseStateNum60WMachineDown,licenseStateNum80WMachineDown
ABC179W_Down,MachineBFunction=1,0,0,3,0,0,0,0,1,0,0
BCD187W_Lack_2_Time,MachineBFunction=1,0,0,3,0,0,0,0,1,0,0
ADB650W_AXE_Aussie,MachineBFunction=1,0,0,0,0,0,0,0,0,1,0
GHS052W_Cambod_Rush,MachineBFunction=1,0,0,3,0,0,0,0,1,0,0

header is constant, and when the corresponding header isn't exist in next row, put 0 in its value

thanks
# 2  
Old 05-27-2013
Using gawk:
Code:
gawk -F, '
        BEGIN {
                h = "name,TM,startBaseCut100WMachineDown,startBaseCut120WMachineDown,\
                startBaseCut40WMachineDown,startBaseCut60WMachineDown,startBaseCut80WMachineDown,\
                licenseStateNum100WMachineDown,licenseStateNum120WMachineDown,licenseStateNum40WMachineDown,\
                licenseStateNum60WMachineDown,licenseStateNum80WMachineDown"
                gsub ( /[ \t]+/, X, h )
                n = split ( h, H )
        }
        ( NR % 2 ) {
                TH[++j] = $0
        }
        !( NR % 2 ) {
                TV[++k] = $0
        }
        END {
                print h
                for ( i = 1; i <= k; i++ )
                {
                        p = split ( TH[i], P )
                        q = split ( TV[i], Q )

                        for ( j = 1; j <= p; j++ )
                                AP[P[j]] = P[j]

                        for ( j = 1; j <= q; j++ )
                                AQ[P[j]] = Q[j]


                        for ( j = 1; j <= n; j++ )
                        {
                                v = ( H[j] in AP ) ? AQ[H[j]] : "0"
                                s = s ? s OFS v : v
                        }
                        print Q[1], s
                        s = ""
                        delete AP
                        delete AQ
                }
        }
' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 05-27-2013
i thought my sybase doesn't support gawk : "gawk: not found"
any nawk?
# 4  
Old 05-27-2013
The delete statement is a gawk extension.

So you can replace below statements in code:
Code:
delete AP
delete AQ

with following code:
Code:
split ( "", AP )
split ( "", AQ )

which is a portable way of clearing out array.

Also replace gawk with awk .
This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-28-2013
when i run this

Code:
awk -F, '
        BEGIN {
                h = "name,TM,startBaseCut100WMachineDown,startBaseCut120WMachineDown,\
                startBaseCut40WMachineDown,startBaseCut60WMachineDown,startBaseCut80WMachineDown,\
                licenseStateNum100WMachineDown,licenseStateNum120WMachineDown,licenseStateNum40WMachineDown,\
                licenseStateNum60WMachineDown,licenseStateNum80WMachineDown"
                gsub ( /[ \t]+/, X, h )
                n = split ( h, H )
        }
        ( NR % 2 ) {
                TH[++j] = $0
        }
        !( NR % 2 ) {
                TV[++k] = $0
        }
        END {
                print h
                for ( i = 1; i <= k; i++ )
                {
                        p = split ( TH[i], P )
                        q = split ( TV[i], Q )

                        for ( j = 1; j <= p; j++ )
                                AP[P[j]] = P[j]

                        for ( j = 1; j <= q; j++ )
                                AQ[P[j]] = Q[j]


                        for ( j = 1; j <= n; j++ )
                        {
                                v = ( H[j] in AP ) ? AQ[H[j]] : "0"
                                s = s ? s OFS v : v
                        }
                        print Q[1], s
                        s = ""
                        split ( "", AP )
            split ( "", AQ )
                }
        }
' OFS=, file

awk: newline in string near line 3
awk: syntax error near line 4
awk: illegal statement near line 4
awk: newline in string near line 6
awk: syntax error near line 10
awk: bailing out near line 10
# 6  
Old 05-28-2013
On SunOS or Solaris use /usr/xpg4/bin/awk or nawk

Also try putting the h variable value defined inside BEGIN block in single line.

I put them in multiple lines using backward slash \ for better readability.
This User Gave Thanks to Yoda For This Post:
# 7  
Old 05-28-2013
superbe!!!!!!!!!!!!!!!!!

why in my solaris, somehow its ok for using nawk, but in another case, it has to be /usr/xpg4/bin/awk?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Multi head/multi window hello world

I am trying to write a large X app. I have successfully modified my xorg.conf to setup 4 monitors on an NVIDIA Quatro5200. I am trying to modify a simple hello world application to open a window on three of the four monitors. depending on the changes to loop the window creation section and event... (2 Replies)
Discussion started by: advorak
2 Replies

2. Shell Programming and Scripting

Multi line extraction based on condition

Hi I have some data in a file as below ****************************** Class 1A Students absent are : 1. ABC 2. CDE 3. CPE ****************************** Class 2A Students absent are : ****************************** Class 3A Students absent are : (6 Replies)
Discussion started by: reldb
6 Replies

3. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

4. Shell Programming and Scripting

Creating unique mapping from multiple mapping

Hello, I do not know if this is the right title to use. I have a large dictionary database which has the following structure: where a b c d e are in English and p q r s t are in a target language., the two separated by the delimiter =. What I am looking for is a perl script which will take... (5 Replies)
Discussion started by: gimley
5 Replies

5. Programming

In unix how we can test or check race condition in a c program by using multi threads

In unix how we can test or check race condition in any c program by using multi thread programming (1 Reply)
Discussion started by: afroze
1 Replies

6. Programming

In unix how we can test or check race condition in c program by using multi threads

In unix how we can test or check race condition in any c program by using multi thread programming (5 Replies)
Discussion started by: afroze
5 Replies

7. UNIX for Dummies Questions & Answers

In unix how we can test or check race condition in a c program by using multi threads

In unix how we can test or check race condition in any c program by using multi thread programming (1 Reply)
Discussion started by: afroze
1 Replies

8. Linux

In unix how we can test or check race condition in c program by using multi threads

In unix how we can test or check race condition in any c program by using multi thread programming (1 Reply)
Discussion started by: afroze
1 Replies

9. UNIX for Dummies Questions & Answers

Multi User Multi Task

Dear Experts Why we always hear that unix operating system is Multi User and Multi task. What does these two means. I have looked at some books and documents but couldn't find aclear explenation. Can we say Windows operating system is also multi user and multi task?? Thanks for your help in... (6 Replies)
Discussion started by: Reza Nazarian
6 Replies

10. UNIX for Dummies Questions & Answers

multi-file multi-edit

Good day! I am trying to learn how to use the "sed" editor, to perform multiple edits on multiple files in multiple directories. I have one script that tries to call up each file and process it according to the edits listed in a second script. I am using a small input text to test these, at... (12 Replies)
Discussion started by: kielitaide
12 Replies
Login or Register to Ask a Question