Need help Badly to convert file format!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help Badly to convert file format!
# 1  
Old 10-15-2013
Need help Badly to convert file format!

Can anyone provide me a unix script to convert file formats. Input and desired output is attached.
# 2  
Old 10-15-2013
could this help you ?
Code:
awk -F"=" '{if(!/^\w/) { a[prev]=a[prev]" "$0} 
             else { a[$1]=$2;b[++count]=$1;prev=$1}} 
             END {for (i=1;i<=count;i++) 
            {printf i==count?b[i]:b[i]",";if(value == "") {value=a[b[i]]}
                 else{value=value=value","a[b[i]]}} printf "\n"value; }' input.txt

# 3  
Old 10-16-2013
Hi Pravin,

I am getting the following error.

Code:
 syntax error The source line is 1.
 The error context is
 {if(!/^\w/) { a[prev]=a[prev]" "$0} else { a[$1]=$2;b[++count]=$1;prev=$1}} END {for (i=1;i<=count;i++) {printf >>>  i== <<<
 awk: The statement cannot be correctly parsed.
 The source line is 1.

Do you thing we need some tweak or correction the awk script? Could you please help me debug this?

Last edited by Franklin52; 10-16-2013 at 10:38 AM.. Reason: Please use code tags for data and code samples
# 4  
Old 10-17-2013
Hi Ravi,

It's working fine at my end.
Code:
$ awk -F"=" '{ if(!/^\w/) { a[prev]=a[prev]" "$0 }
else { a[$1]=$2;b[++count]=$1;prev=$1 }
}
END {
for (i=1;i<=count;i++)
{printf i==count?b[i]:b[i]",";
if(value == "") {value=a[b[i]]}
else{value=value=value","a[b[i]]}
}
printf "\n"value"\n";
}' input.txt

Output
Code:
FORMAT,VERSION,STATUS,MCOD,MCODSEQ,DATE,ORIG,BRAND,FAMILY,FAMCOD,APPL,HOUSING,OPTICS,BLID,LAMP,LAFLUX,NLPS,LAMPCOL,INPW,INVO,GEOTYPE,GEOL1,GEOL2,GEOL3,OPTTYPE,OPTL1,OPTL2,OPTL3,CIECODE,COLORFAC,CORR,PTYP,BANGLE,TLME,LUBA,SYMCON,SYMPLANE,NCON,NPLA,CONA,PLANA,ITABLE
PHILLUM,2.0,R,HOF5814,00,2008-08-21,WLD,Philips,FBS 301,FBS301,INTL,FBS301,A,HF,PL-T/4P26W,1800,1,830,26,230,3,0.09,0.22,0.22,3,0.05,0.16,0.17,36 67 89 98 57,0,1,C,0,0,1000,4,1,23,13,         0.      5.     10.     15.     20.     25.     30.     35.     40.              45.     50.     55.     60.     65.     70.     75.     80.     85.              90.     95.    100.    105.    110.,        0.     15.     30.     45.     60.     75.     90.    105.    120.             135.    150.    165.    180.,     87.3   171.4   242.9   297.6   322.8   325.6   306.8   284.5   273.4            272.2   270.2   264.5   241.7   231.3   212.0   180.4   151.8   106.3             64.7    24.5    10.1     3.0       0             87.3   168.1   238.1    293.   319.7   325.3   308.5    284.   270.5            268.5   265.4   261.0    239.   227.8   207.8   175.6   145.3   102.3             62.3    23.5     9.7     2.9     0.1             87.3   159.4   224.5   279.5    308.     316    307.   278.7   260.5            253.5   248.3   247.1   227.9   210.9    190.   159.1   124.7    86.3             51.5     18.     7.2     2.1     0.4             87.3   146.2   201.0   247.5   280.9   293.9   297.4   277.4   252.2            236.9   223.0   217.4   205.8    188.   166.2     136    98.1    61.8             32.7    10.9     3.6     1.1     0.3             87.3   127.2   168.1   202.4   230.7   251.2   262.5     256    240.            221.2   199.4   184.1   165.1     150   130.7   101.8    67.7    35.5             15.9     4.5     1.2     0.5     0.2             87.3   106.5    128.   145.9   160.4   171.8   182.5   187.0   184.1            182.4   169.1   150.9    128.   104.1    81.0    62.9     38.    13.1               2.      0.     0.2     0.1       0             87.3    86.9    87.4    85.7    83.6    81.3    79.7    77.6    77.1             76.1    70.4    66.5    57.8    45.6    33.1      24    13.4     3.5              0.3     0.1     0.2     0.0       0             87.3    71.1    60.2    49.3    39.5    31.1    24.2    19.0    15.4             13.8    12.7     11.     8.5     6.3     4.5     3.0     1.8     0.8              0.1     0.1     0.0     0.1     0.0             87.3    60.1    43.3    30.1    20.5     13.     8.6     5.5     3.9              2.9      2.     2.0     1.6     1.3     1.2     1.1     1.0     0.6              0.1     0.1     0.1     0.1     0.0             87.3    52.1    32.7    20.2    12.1     7.1     4.3     2.9     2.1               1.     1.4     1.2     1.0     1.0     0.8     0.9     0.8     0.5              0.1     0.0     0.1     0.1     0.0             87.3    46.6    26.5    15.0     8.2     4.6     3.0     2.2     1.7               1.     1.1     1.1     1.0     0.7     0.7     0.8      0.     0.3              0.1     0.0     0.1     0.0     0.0             87.3    43.3    23.1    12.3      6.     3.6     2.5     1.9     1.4              1.2     1.1     1.1     0.9     0.4     0.4     0.4     0.3     0.2              0.1     0.0     0.1     0.1     0.0             87.3    42.4    22.1    11.5     5.8      3.     2.3     1.8     1.3              1.1     1.0     1.1     0.6     0.3     0.2     0.2     0.1     0.1                0     0.1     0.1     0.1     0.1

# 5  
Old 10-17-2013
@ pravin27 it seems he might be using mawk I guess
# 6  
Old 10-17-2013
I am still getting the same error. Is it probably because quoting standards? Does it vary with the shell? I am AIX machine...Smilie
# 7  
Old 10-17-2013
It may be that it doesn't like the conditional expression (although it's POSIX, as far as I remember). Try:
Code:
$ awk -F"=" '{ if(!/^\w/) { a[prev]=a[prev]" "$0 }
else { a[$1]=$2;b[++count]=$1;prev=$1 }
}
END {
for (i=1;i<=count;i++)
{
if (i==count) {printf b[i]} else {printf b[i]","}
if(value == "") {value=a[b[i]]}
else{value=value=value","a[b[i]]}
}
printf "\n"value"\n";
}' input.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert the below file to csv format

Hi , i want to change this question, i will post soon.. (6 Replies)
Discussion started by: srikanth2567
6 Replies

2. Shell Programming and Scripting

Convert UNIX file format to PC format

Hi All, Is there any way to convert a file which is in UNIX format to a PC format.... Flip command can be used , apart form this command can we have any other way.... like usinf "awk" etc ..... main purpose of not using flip is that my Kshell doesnot support this comamnd.... (2 Replies)
Discussion started by: Samtel
2 Replies

3. UNIX for Dummies Questions & Answers

Convert UNIX file format to PC format

Hi All, Is there any way to convert a file which is in UNIX format to a PC format.... Flip command can be used , apart form this command can we have any other way.... like usinf "awk" etc ..... main purpose of not using flip is that my Kshell doesnot support this comamnd.... (1 Reply)
Discussion started by: Samtel
1 Replies

4. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

5. Shell Programming and Scripting

Convert Epoch time format to normal date time format in the same file

I have a file named "suspected" with series of line like these : {'protocol': 17, 'service': 'BitTorrent KRPC', 'server': '219.78.120.166', 'client_port': 52044, 'client': '10.64.68.44', 'server_port': 8291, 'time': 1226506312L, 'serverhostname': ''} {'protocol': 17, 'service': 'BitTorrent... (3 Replies)
Discussion started by: rk4k
3 Replies

6. UNIX for Dummies Questions & Answers

To convert multi format file to a readable ascii format

Hi I have a file which has ascii , binary, binary decimal coded,decimal & hexadecimal data with lot of special characters (like öƒ.ƒ.„İİ¡Š·œƒ.„İİ¡Š· ) in it. I want to standardize the file into ASCII format & later use that as source . Can any one suggest a way a logic to convert such... (5 Replies)
Discussion started by: gaur.deepti
5 Replies

7. Shell Programming and Scripting

convert/format a file

CSV---- fixed length fiile format Can any suggest a mechanism for converting a CSV to fixedlength,the file is expected to be of size 50000lines, :mad: CSV file contains address feilds of variable length (17 Replies)
Discussion started by: anumkoshy
17 Replies

8. UNIX for Dummies Questions & Answers

Convert UTF8 Format file to ANSI format

:confused: Hi i am trying to convert a file which is in UTF8 format to ANSI format i tried to use the function ICONV but it is throwing error Function i used it as $ iconv -f UTF8 -t ANSI filename Error iam getting is NOT Supported UTF8 to ANSI please some help me out on... (9 Replies)
Discussion started by: rajreddy
9 Replies

9. UNIX for Advanced & Expert Users

Convert UTF8 Format file to ANSI format

:) Hi i am trying to convert a file which is in UTF8 format to ANSI format i tried to use the function ICONV but it is throwing error Function i used it as $ iconv -f UTF8 -t ANSI filename Error iam getting is NOT Supported UTF8 to ANSI please some help me out on this.........Let me... (1 Reply)
Discussion started by: rajreddy
1 Replies

10. Shell Programming and Scripting

How to convert file format ?

Hi guys, I have a text file like in following format, IMSI:1 MSISDN:44569098 Data1=prov Data2=yes Data3=not Data4=yes Data5=yes Data6=yes IMSI:2 MSISDN:44569099 Data1=yes Data2=not Data3=prov Data4=prov (3 Replies)
Discussion started by: maheshsri
3 Replies
Login or Register to Ask a Question