Convert flat file to csv


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Convert flat file to csv
# 8  
Old 09-10-2014
Here is an awk approach:-
Code:
awk -F= '
        BEGIN {
                j = 1
        }
        NR == FNR {
                T[$1]
                next
        }
        !flag {
                for ( k in T )
                        S[k] = ++c
                flag = 1

        }
        {
                if ( S[$1] < S[p] )
                        ++j
                A[$1,j] = $2
                p = $1

        }
        END {
                for ( k in S )
                        s = s ? s OFS k : k
                print s
                s = ""
                for ( i = 1; i <= j; i++ )
                {
                        for ( k in S )
                                s = s ? s OFS A[k,i] : A[k,i]
                        print s
                        s = ""
                }
        }
' OFS=, file file

# 9  
Old 09-11-2014
Adapting your own approach:
Code:
awk -F"="       'BEGIN  {print "a,b,c,d";}
                 /a/    {if (NR>1) print A,B,C,D; A=B=C=D=""; A=$2}
                 /b/    {B=$2}
                 /c/    {C=$2}
                 /d/    {D=$2}
                 END    {print A,B,C,D}
                ' OFS="," file
a,b,c,d
1,2,3,
4,2,,3
3,,4,

Your sample output has one comma too many...

Last edited by RudiC; 09-11-2014 at 03:27 PM.. Reason: missed a line break before output ...
This User Gave Thanks to RudiC For This Post:
# 10  
Old 09-11-2014
When using a Solaris/SunOS system, you need to use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk for RudiC's or Yoda's code to work.
# 11  
Old 09-11-2014
RuDiC

Thanks

I have used your approach and that works fine.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting csv file to flat file

Hi All, I have a csv file which is comma seperated. I need to convert to flat file with preferred column length country,id Australia,1234 Africa,12399999 Expected output country id Australia 1234 Africa 12399999 the flat file should predefined length on respective... (8 Replies)
Discussion started by: rohit_shinez
8 Replies

2. Shell Programming and Scripting

reading a csv file and creating a flat file

hi i have written a script for reading a csv file and creating a flat file, suggest if this script can be optimized #---------------- FILENAME="$1" SCRIPT=$(basename $0) #-----------------------------------------// function usage { echo "\nUSAGE: $THIS_SCRIPT file_to_process\n"... (3 Replies)
Discussion started by: mprakasheee
3 Replies

3. Shell Programming and Scripting

How to convert a xls file to csv?

Hi, My requirement is to convert the xls to csv file with utf-8 conversion. Is there any way please suggest me. Thanks, Raja (4 Replies)
Discussion started by: cnraja
4 Replies

4. Shell Programming and Scripting

Flat file to csv conversion

Hi Guy's can someone help me in converting the following I have a flat text file which has several thousand lines which I need to convert to a csv it's got a consistent format but basically want every time it hit's txt to create a new line with the subsequent lines comma delimited for example ... (6 Replies)
Discussion started by: p1_ben
6 Replies

5. 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

6. Programming

convert text file to csv

hi all, i have a select query that gives me the output in the following way... SYSTYPE -------------------------------------------------------------------------------- Success Failures Total RFT ---------- ---------- ---------- ---------- TYP 1 0 ... (3 Replies)
Discussion started by: sais
3 Replies

7. Shell Programming and Scripting

csv file to array, match field1, replace in flat file field1 to field2

Hello, i am bit stuck with making script for automatic procedure. Case: Two files. One is flat file, other is csv file. csv file has two column/fields with comma delimited data. Here is what i need (explained way) CSV file: field1 | field2 "hello","byebye" "hello2","byebye2"... (23 Replies)
Discussion started by: frankie_konin
23 Replies

8. Shell Programming and Scripting

Convert case on specified position of flat file

Please help Need a script which will do the following : Search on fixed width file , go to position (25,2) which means 25th and 26th position, Find if there are any char in lower case: For example 25,2 can be (9T) or (9w) or (Ww) or (wW)....The two positions can be numeric or alpha...no... (13 Replies)
Discussion started by: ssantoshss
13 Replies

9. Shell Programming and Scripting

Awk to convert a flat file to CSV file

Hi , I have a file with contents as below: Contract Cancellation Report UARCNCL LOS CODE DATE REAS TYPE AMOUNT AMOUNT LETTER BY ========= ======= ==== ==== ==== ========= ==== ==== 8174739 7641509 1S NONE CRCD 30-JUN-2008 NPAR N .00 .00 CCAN 8678696 8091709 1S NONE DDEB 30-JUN-2008... (14 Replies)
Discussion started by: rkumudha
14 Replies

10. Shell Programming and Scripting

Need help to convert Flat file to HTML

Hello I need help to convert flat file data to HTML Table format. I am generating everyday Flat file and want to convert into HTML Table format. The format of my file is: version host Total YRS NO APPS PSD 10 Sun 30 2 4 6 7 and flat... (11 Replies)
Discussion started by: getdpg
11 Replies
Login or Register to Ask a Question