Convert flat file to csv


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Convert flat file to csv
# 1  
Old 09-10-2014
Linux Convert flat file to csv

Hi I have a file like this:
Code:
 
a=1
b=2
c=3
a=4
b=2
d=3
a=3
c=4

How can I change this to csv format
Code:
a,b,c,d
1,2,3,,
4,2,,3
3,,4,,

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data. Thanks
# 2  
Old 09-10-2014
Welcome sandip_2014,
I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

it would be useful to know the relationship between the input and output. There could be many ways of getting the output from the input, but only one that matches the way that will make it reusable for you. There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


kind regards,
Robin
# 3  
Old 09-10-2014
Nope this is not home work..

I have tried using awk

Code:
awk -F"=" 'BEGIN {print "a,b,c,d";}/a/{line=line$2","}/b/{line=line$2","}/c/{line=line$2","}/d/{line=line$2;print line}' test.log>test.csv

But that is not working.... the csv is coming jumbled up

I am solaris10 sparc

Last edited by joeyg; 09-10-2014 at 11:35 AM.. Reason: Please wrap commands and data within CodeTags
# 4  
Old 09-10-2014
Can you explain the logic of the conversion you want to make? I don't see it clearly from the input-->output you have shown.


Robin
# 5  
Old 09-10-2014
Hi Robin

The input file is
Code:
a=1
b=2
c=3
a=4
b=2
d=3
a=3
c=4

and I want the output as a csv
Code:
a,b,c,d
1,2,3,,
4,2,,3
3,,4,,

In the AWK I tried to use = as delimiter and print all data next to it with a comma separator but that did not help.

Last edited by rbatte1; 09-10-2014 at 12:04 PM.. Reason: Added CODE tags
# 6  
Old 09-10-2014
Please use code tags as required by forum rules!

Is it always 4 columns, i.e. d is the last one? And, a new line always starts with an a= entry? Or how to tell where line switches are?
# 7  
Old 09-10-2014
RudiC

Yes.

It always ends in
Code:
d

and starts with
Code:
 a=


Last edited by sandip_2014; 09-10-2014 at 12:25 PM..
 
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