Format column datas


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format column datas
# 1  
Old 03-22-2016
Format column datas

I have some data like this:
Code:
4258092
TRXCODE     a   19 
CARDNBR     a   10
PINFLAG      a    6
FUISSUER    a   12
PRODUCT     a   24
STATE         n    1

I want out put this format:
Code:
<?xml version="1.0" encoding="GB2312"?>
<convGrp>
    <grpid>4258092</grpid>
    <script>
    <![CDATA[
        SET = FILL($TRXCODE  ,CHR(32),R,19)
        SET = FILL($CARDNBR  ,CHR(32),R,10)
        SET = FILL($PINFLAG   ,CHR(32),R,6)
        SET = FILL($FUISSUER ,CHR(32),R,12)
        SET = FILL($PRODUCT  ,CHR(32),R,24)
        SET = FILL($STATE      ,CHR(48),L,1)
    ]]>
    </script>
</convGrp>

what does this mean is: if the first column is char(a) set it right with space and length(acii 32)if the first column is number(n) set it left with 0and length(acii 48)
pls help! thank you !
# 2  
Old 03-22-2016
try:
Code:
awk '
BEGIN {c["a"]="CHR(32),R,"; c["n"]="CHR(48),L,";}
NF==1 {
   print "<?xml version=\"1.0\" encoding=\"GB2312\"?>";
   print "<convGrp>";
   print "   <grpid>" $1 "</grpid>";
   print "   <script>";
   print "   <![CDATA[";
}
NF==3 && c[$2] {
   print "      SET = FILL($" $1 "," c[$2] $3 ")";
}
END {
   print "   ]]>";
   print "   </script>";
   print "</convGrp>";
}
' data

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 03-22-2016
thank you thank you very much! but what does c[] mean?
# 4  
Old 03-22-2016
Quote:
Originally Posted by hhdzhu
I have some data like this:
Code:
4258092
TRXCODE     a   19 
CARDNBR     a   10
PINFLAG      a    6
FUISSUER    a   12
PRODUCT     a   24
STATE         n    1

I want out put this format:
Code:
<?xml version="1.0" encoding="GB2312"?>
<convGrp>
    <grpid>4258092</grpid>
    <script>
    <![CDATA[
        SET = FILL($TRXCODE  ,CHR(32),R,19)
        SET = FILL($CARDNBR  ,CHR(32),R,10)
        SET = FILL($PINFLAG   ,CHR(32),R,6)
        SET = FILL($FUISSUER ,CHR(32),R,12)
        SET = FILL($PRODUCT  ,CHR(32),R,24)
        SET = FILL($STATE      ,CHR(48),L,1)
    ]]>
    </script>
</convGrp>

what does this mean is: if the first column is char(a) set it right with space and length(acii 32)if the first column is number(n) set it left with 0and length(acii 48)
pls help! thank you !
Hello hhdzhu,

Welcome to forums, hope you will enjoy learning and sharing knowledge here. Following may help you in same.
Code:
awk -vTAB="\t" -vTTAB="\t\t" -vparan=")" -vCHAR1="      ,CHR(48),L," -vCHAR="  ,CHR(32),R," -vSET="SET = FILL($" -vGRPID="<grpid>,</grpid>" -vSCRIPT="<script>,</script>" -vCOVGROUP="<convGrp>,</convGrp>" -vCDATA="<[CDATA[,]]>" 'BEGIN{print "<?xml version="1.0" encoding="GB2312"?>"} {split(GRPID, A,",");split(SCRIPT, B,",");split(COVGROUP, C,",");split(CDATA, D,",");if(NR==1){print C[1] ORS TAB A[1] $0 A[2] ORS TAB B[1];next} {if($2=="a"){Q=CHAR};if($2=="n"){Q=CHAR1};print TTAB D[1] ORS TTAB SET $1 Q $NF paran}}END{print TAB D[2] ORS "\t" B[2] ORS C[2]}'  Input_file

Output will be as follows.
Code:
<?xml version=1 encoding=?>
<convGrp>
        <grpid>4258092</grpid>
        <script>
                <[CDATA[
                SET = FILL($TRXCODE  ,CHR(32),R,19)
                <[CDATA[
                SET = FILL($CARDNBR  ,CHR(32),R,10)
                <[CDATA[
                SET = FILL($PINFLAG  ,CHR(32),R,6)
                <[CDATA[
                SET = FILL($FUISSUER  ,CHR(32),R,12)
                <[CDATA[
                SET = FILL($PRODUCT  ,CHR(32),R,24)
                <[CDATA[
                SET = FILL($STATE      ,CHR(48),L,1)
        ]]>
        </script>
</convGrp>

If you have any more requirements on same then please do let us know the sample Input_file and expected output with all conditions, hope this helps you.
EDIT: Adding a non-one liner form for solution on same now too.
Code:
awk -vTAB="\t" -vTTAB="\t\t" -vparan=")" -vCHAR1="      ,CHR(48),L," -vCHAR="  ,CHR(32),R," -vSET="SET = FILL($" -vGRPID="<grpid>,</grpid>" -vSCRIPT="<script>,</script>" -vCOVGROUP="<convGrp>,</convGrp>" -vCDATA="<[CDATA[,]]>" 'BEGIN{
                                                                        print "<?xml version="1.0" encoding="GB2312"?>"
                                                                 }
                                                                 {
                                                                        split(GRPID, A,",");
                                                                        split(SCRIPT, B,",");
                                                                        split(COVGROUP, C,",");
                                                                        split(CDATA, D,",");
                                                                        if(NR==1){
                                                                                        print C[1] ORS TAB A[1] $0 A[2] ORS TAB B[1];
                                                                                        next
                                                                                 }
                                                                 {
                                                                        if($2=="a"){
                                                                                        Q=CHAR
                                                                                   };
                                                                        if($2=="n"){
                                                                                        Q=CHAR1
                                                                                   };
                                                                        print TTAB D[1] ORS TTAB SET $1 Q $NF paran
                                                                 }
                                                                 }
                                                           END   {
                                                                        print TAB D[2] ORS "\t" B[2] ORS C[2]
                                                                 }
                                                           '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 03-22-2016 at 12:22 PM.. Reason: Added a non-one liner form of solution now.
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 03-22-2016
thank you for the help!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

How to display in column format?

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I would like to know how to display my output to this format below: 5000 Bash 300 zsh 0 sh ... (4 Replies)
Discussion started by: scopiop
4 Replies

2. UNIX for Dummies Questions & Answers

Comparing datas in two excel file

Hi All, i have two excel sheets with same column name as below col1 col2 col3 ---- ---- ----- 1 121 156 24 456 788 45 444 777 765 32 77 col1 col2 col3 ---- ---- ----- 24 456 ... (1 Reply)
Discussion started by: arunmanas
1 Replies

3. Shell Programming and Scripting

Print separated datas

Hi People !! I need your help. I have a a txt file "example" with it datas. 11:35 10/12/2003 10.10.10.1 God.com 5 country Responsable of ASN 11:37 12/12/2003 10.10.10.1 FATHER.COM 5 country Responsable of ASN 11:40 14/12/2003 10.10.10.3 www.mother 6... (6 Replies)
Discussion started by: ras
6 Replies

4. Shell Programming and Scripting

Format Column

I need a script that takes it input wither it be from a file or inputed as variables and sort that into three columns. Thanks (3 Replies)
Discussion started by: prottheman
3 Replies

5. Shell Programming and Scripting

arranging datas if input file is not having also...!!

hi, my input file is containg uid, eriMaster ,eriResign, ericontry, dept. some of the uid are not having all info. out put should include all info irrespctive of datas of input file if any one data is missing, then it has to print Null or zero..then continue with the existing one. here... (0 Replies)
Discussion started by: hegdeshashi
0 Replies

6. Shell Programming and Scripting

How to format a column

Hi everybody I want to manipulate the output of this command: df -h to something like this: df -h | awk '{print"\t\t "$1"\t\t"$6"\t\t"$2"\t\t" $4}' | sed 's/Mounted/Mount Point/g' | sed 's/Avail/Free Space/g'but my problem is: if for example the mounting point is a long name everything beside... (3 Replies)
Discussion started by: ialbert
3 Replies

7. Shell Programming and Scripting

AWK CSV to TXT format, TXT file not in a correct column format

HI guys, I have created a script to read 1 column in a csv file and then place it in text file. However, when i checked out the text file, it is not in a column format... Example: CSV file contains name,age aa,11 bb,22 cc,33 After using awk to get first column TXT file... (1 Reply)
Discussion started by: mdap
1 Replies

8. UNIX for Dummies Questions & Answers

Format a column of text

Hi I have a LONG ! column of text and I would like to format this into several columns. so from this abcd efgh ijkl mnop qrst uvwx yz12 to this abcd efgh ijkl mnop qrstu vwx yz12 (2 Replies)
Discussion started by: mlucas
2 Replies

9. Shell Programming and Scripting

Blank characters between Datas

Hello, I read a file whose in lines are datas and between thses datas there is blank characters (10, 12 or 5 or 1 .......) So when i use the command while read line in the script(see under) there is also only one character between the datas and the others blank characters are not here. ... (3 Replies)
Discussion started by: steiner
3 Replies

10. Shell Programming and Scripting

select datas from an input file

I have a file containing a list of references and I want to run a script that will make the same action for each reference. The input file changes every hour, it's why I want to use a script that can read in a file, record by record, and run a specific action for the reference readed. Thanks... (1 Reply)
Discussion started by: dde
1 Replies
Login or Register to Ask a Question